Thursday, April 16, 2015

Design Patterns

Design patterns are useful because they provide a pre-formulated solution to problems based on the experience of other programmers. This can save you a lot of time.
Design patterns also help in communication of a programming solution, and provide a common technical vocabulary between programmers.
Design pattern and Framework are not same thing. Design patterns are more like general guidelines on how to solve specific programming problems, but they do not specify the detailed code that’s necessary to solve those problems.
 Of course, using the wrong design pattern can have a very negative effect on your code. So, it takes good judgement to use the correct design pattern. The term “anti-pattern” is used to describe a poor programming practice that leads to ineffective code.

In general, a pattern has four essential elements:
1.The pattern name is a handle we can use to describe a design problem
2.The problem describes when to apply the pattern. It explains the problem and its context.
3.The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations.
4.The consequences are the results and trade-offs of applying the pattern.


We classify design patterns by two criteria (Table 1.1). The first criterion,  called purpose, reflects what a pattern does. Patterns can have either creational,  structural, or behavioral purpose. Creational patterns concern the process of  object creation. Structural patterns deal with the composition of classes or  objects. Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility.

The second criterion, called scope, specifies whether the pattern applies primarily to classes or to objects. Class patterns deal with relationships between classes and their subclasses. These relationships are established through inheritance, so they are static—fixed at compile-time. Object patterns deal with object relationships, which can be changed at run-time and are more dynamic. Almost all patterns use inheritance to some extent. So the only patterns labeled "class patterns" are those that focus on class relationships. Note that most patterns are in the Object scope.

Creational class patterns defer some part of object creation to subclasses, while Creational object patterns defer it to another object. The Structural class patterns use inheritance to compose classes, while the Structural object patterns describe ways to assemble objects. The Behavioral class patterns use inheritance to describe algorithms and flow of control, whereas the Behavioral object patterns describe how a group of objects cooperate to perform a task that no single object can carry out alone.

No comments: