Pages

Friday, February 11, 2011

Overview of popular software design patterns

Singleton
Ensure that only one instance of a class is created. To implement this, make the constructor protected (unit tests may need to access it), have a private static member which is an instance of itself, and have a get method that instantiates the class only if one does not already exist. 

Use of singletons can be abused. They are a little like global variables. Systems relying on global state hide their dependencies. Some arguments against using it: 
  • It promotes tight coupling between classes 
  • It violates the single responsibility principle - a class should not case whether it is a singleton.
They can be fairly safely used when they don't contain any mutable state. An example of an appropriate use of a singleton may be a global resource manager.

Factory
Creates objects without exposing the instantiation logic to the client and refers to the newly created object through a common interface. A simple implementation would be to have an interface and some implementing classes. The factory will return an instance of the superclass. Which subclass it is will depend on parameters passed in to the creation/get method.

Factory Method
Defines an interface for creating objects, but lets subclasses decide which class to instantiate and refers to the newly created object through a common interface.

MVC
Separates the model, view, and controller, or presentation, logic, and data.

DAO
Abstracts and encapsulates all access to the data source. The DAO manages the connection with the data source to obtain and store data.

Facade
Provides a unified, convenient interface to a set of existing interfaces, potentially hiding some components/interfaces. It provides flexibility to change/replace "hidden" subsystems including interfaces.

Prototype
Specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype.

Strategy
Defines a family of algorithms, encapsulating each one and making them interchangeable. The strategy pattern lets the algorithm vary independently from clients that use it.

Observer
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Command
Encapsulates a request in an object allowing the parameterisation of clients with different requests and allows saving the requests in a queue.

Visitor
Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Decorator
Adds additional responsibilities dynamically to an object.

Flyweight
Uses sharing to support a large number of objects that have part of their internal state in common where the other part of their state can vary.

Proxy
Provides a placeholder for an object to control references to it.

Bridge
Increases flexibility by letting abstraction and implementation evolve independently. This is achieved by having separate layers for abstraction and implementation, with a fixed implementation interface.



Thursday, February 10, 2011

J2EE Overview

Yes, there are so many other sites out there that provide J2EE Overviews but this is probably the most breif but yet complete one you'll find.

J2EE defines standards for developing multitier enterprise applications. It simplifies enterprise applications by basing them on standardised, modular components, and providing a set of services to those components. It handles many details of application behaviour automatically, without requiring complex programming.

Enterprise JavaBeans
EJB is a standard distributed object framework and component model. It defines several types of components: session beans, entity beans, message driven beans, which simplify application development by concealing application complexity and enabling the component designer to focus on business logic.

Servlets and JSPs
Servlets and Java Server Pages are complementary APIs, both providing a means for generating dynamic web content. Servlets are Java programs implementing the javax.servlet.Servlet interface and running in a Web/App server's servlet engine. JSPs contain a mixture of HTML and java scripts, JSP elements, and directives, and are compiled into Servlets by the JSP engine.

JDBC
JDBC is a set of interfaces allowing Java applications to access any database.

RMI
RMI is an API which allows Java objects to communicate remotely with other objects. Its equivalent from OMG is CORBA.

IDL
IDL is a standard platform-independent language used to define interfaces that object implemnetation provide and client objects call. It allows java objects to communicate with other objects in any language.

JMS
JMS API is a messaging standard that allows J2EE components to create, send, receive, and read messages. It enables distributed communication between components. The addition of the JMS API enhances the J2EE platform by simplifying enterprise development, allowing loosely coupled, reliable, asynchronous interactions among J2EE components and legacy systems capable of messaging.

JTA
JTA allows J2EE components to perform distributed transactions.

JavaMail
JavaMail API allows Java components to send and receive emails.

JAXP (includes JAXB which is sometimes mentioned separately?)
Java API for XML Processing allows java applications to parse and transform XML documents.

JNDI
Java Naming and Directory Interface is a protocol which provides a standard API to access naming and directory services. It allows Java applications to find any necessary resource in a standard way.


Why use J2EE?
  • It is a standardised and reliable software architecture
  • that gives a lot of flexibility
  • is well documented,
  • with low level services already implemented.

Should foreign keys always be indexed?

In my opinion, mostly yes. The query optimizer will make a decision on whether it will be faster to use it or not. Indexes may not be of much use for low cardinality reference columns and will not be used by the optimiser.

On a Star Schema there may be some benefit from indexing low cardinality columns. This will give the query optimiser the option of using index intersection.

Foreign key indexing may not be desirable for purely data warehousing tables that are frequently batch loaded. The index write traffic will be large if there are many indexed columns and it may be necessary to disable foreign keys and indexes for these kinds of operations.

A more obscure reason for ensuring foreign keys are always indexed is the following - A delete from the parent table may lock the child table, which may result in a deadlock.

Unexpected behaviour with JUnit ExpectedException

The JUnit ExpectedException allows specification of expected exception types and messages. One would think that code throwing the exception would carry on executing any lines following it as the exception is expected. This is not the case.

The solution is to make sure that the code throwing the exception is the last line of the method.
 
Powered by Blogger