8/07/2011

Design Pattern - Strategy

Definition
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

UML Class Diagram

 
Strategy: when and where use it 
- The Strategy design pattern is widely used. Its intent is to encapsulate alternative strategies for a particular operation. The Strategy pattern is a "plug-and-play" pattern. The client calls a method on a particular interface which can be swapped out with any other Strategy class that implements the same interface.  Strategy is useful in many different scenarios.  An example is credit card processing.  If a customer on an Shopping site prefers to pay with PayPal over 2Checkout the application can simply swap the PayPal strategy class out for the 2Checkout strategy class. 

- If the Strategy interface has only a single method you can simplify the implementation by using a delegate rather than an interface. If you think about it, a delegate is a special case of the Strategy pattern. Therefore, you could argue that .NET?s event model (which uses delegates as eventhandlers) is built also on the Strategy pattern. 

Sample
- Product

- IFilterStrategy

- Concrete strategy

- Client class


Ref: Gang of Four tutorial

Design Pattern - Iterator

Definition
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.. (implement methods to access to collection as a part of Aggregate class)

UML Class Diagram

Iterator: when and where use it
- A common programming task is to traverse and manipulate a collection of objects.  These collections may be stored as an array, a list, or perhaps something more complex, such as a tree or graph structure.  In addition, you may need access the items in the collection in a certain order, such as, front to back, back to front, depth first (as in tree searches),  skip evenly numbered objects, etc.  The Iterator design pattern solves this problem by separating the collection of objects from the traversal of these objects by implementing a specialized iterator class. 

Sample
- Product

- Product Collection
 
- Client


Ref: Gang of Four tutorial

Design Pattern - Interpreter

Definition
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language..

UML Class Diagram

Interpreter: when and where use it
- If your applications are complex and require advanced configuration you could offer a scripting language which allows the end-user to manipulate your application through simple scripting. The Interpreter design pattern solves this particular problem – that of creating a scripting language that allows the end user to customize their solution.  

- The truth is that if you really need this type of control it is probably easier and faster to use an existing command interpreter or expression evaluator tool out of the box. VBScript comes to mind, as well Microsoft?s new support for .NET Dynamic Languages.

Ref: Gang of Four tutorial