Monday, February 8, 2010

Java Design Patterns

“Pattern” as the name suggests, means series of events occurring in a definite order. The patterns can be found in Java and J2ee technologies also. Many a times, we find that there is a particular way of tackling a problem. This way is easy and has been used many times successfully by a number of people earlier also. This method becomes a pattern.

“Design patterns are recurring solutions to design problems.”

Patterns: According to commonly known practices, there are 23 design patterns in Java.

These patterns are grouped under three heads:

  1. Creational Patterns
  2. Structural Patterns
  3. Behavioral Patterns

Creational Patterns :

All the creational patterns define the best possible way in which an object can be instantiated. These describes the best way to CREATE object instances. The object instance in Java can be created using a new operator.

Book book = new Book ();

The new Operator creates the instance of an object, but this is hard-coding.Creating good software is difficult and so, hard coding is the last thing one should do. Also, at times the very nature of the object which is created can change according to the nature of the program. In such scenarios, we can make use of patterns to give this a more general and flexible approach.

There are five types of Creational Patterns.

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Pattern
  4. Prototype Pattern
  5. Singleton Pattern

Structural Patterns:

Structural Patterns describe how objects and classes can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe abstraction with the help of inheritance and how it can be used to provide more useful program interface. Object patterns, on other hand, describe how objects can be associated and composed to form larger, more complex structures.

There are seven structural patterns described. They are as follows:

  1. Adapter Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. Flyweight Pattern
  7. Proxy Pattern

Behavioral Patterns:

Behavioral patterns are those which are concerned with interactions between the objects. The interactions between the objects should be such that they are talking to each other and still are loosely coupled. The loose coupling is the key to n-tier architectures. In this, the implementation and the client should be loosely coupled in order to avoid hard-coding and dependencies.

The behavioral patterns are:

  1. Chain of Resposibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Momento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Pattern
  11. Visitor Pattern

No comments:

Post a Comment