Wednesday, July 20, 2011

Design Pattern

Life is full of problems. Some problems occur frequently and are common. Keeping solutions to such problems in mind is good. This saves time in solving the problem. At least you will be knowing how to approach the problem. Its not necessary that you approach it the same way or use the same solution but then you will be aware of the known solution that may help you to make your own solutions... better one :)

Design patterns are no different except that these are intended for problems in software design. They are reusable solutions for common problems. It may be a simple description of solution or a template. It is language independent. They are not complete solutions since they only provide description or template which needs to be converted into code for final use.

In simple words design patterns are recurring solutions to design problems. The documentation format(explaining about the design pattern) as given by Erich Gamma,Richard Helm, Ralph Johnson and John Vlissides (collectively known as the "Gang of Four", or GoF for short) in their book Design Patterns contains:
1.  Pattern Name and Classification: A descriptive and unique name that helps in identifying and referring to the pattern.
2.  Intent: A description of the goal behind the pattern and the reason for using it.
3.  Also Known As: Other names for the pattern.
4.  Motivation (Forces): A scenario consisting of a problem and a context in which this pattern can be used.
5.  Applicability: Situations in which this pattern is usable; the context for the pattern.
6.  Structure: A graphical representation of the pattern. Class diagrams and Interaction diagrams may be used for this purpose.
7.  Participants: A listing of the classes and objects used in the pattern and their roles in the design.
8.  Consequences: A description of the results, side effects, and trade offs caused by using the pattern.
9.  Implementation: A description of an implementation of the pattern; the solution part of the pattern.
10. Sample Code: An illustration of how the pattern can be used in a programming language.
11.Known Uses: Examples of real usages of the pattern. 
12. Related Patterns: Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns.

There are many design patterns known so far out of which 23 java design patterns are commonly practiced. They are grouped as follows

Well let me give you some brief idea on them. We will see more on it later.
Creational patterns are the ones that deal with object creation mechanism. In java, creating an object by simple statement Class_A a = new Class_A(); is hardcoding and should be avoided for having a quality software. So we should choose the object creation mechanism depending on the situations or scenario and requirement. Creational patterns include many patterns that will help us in doing this. 

Behavioral patterns define common communication patterns between objects to provide flexibility in communication. So basically it deals with object interactions. The objects should be interacting with loose coupling.

Structural patterns are patterns for realizing relationship between entities. They are for describing how objects, classes and other entities are associated with each other and therefore giving us a complete structure in abstraction.

Coffee Break
Design Patterns are for making use of solutions(predefined or known) for commonly known problems.
They are language independent.
They are description or template of solution.
They are incomplete solutions(need to be transformed into code).
Grouped under creational, structural and behavioral.