Monday 4 November 2013

Strategy Pattern

Factory and Strategy Pattern
In nutshell, Strategy Pattern allow us to have a configurable behavior of the object by composition. We can change this behavior of the object at deployment time or at runtime. Usually this behavior is provided when the object is created.
Factory is very handy design pattern, the whole idea of dependency injection is related to it. Usually dependencies are the variable part of our applications we want to defer it to deployment time or to execution time. At deployment time these dependencies are put in the configuration files and some Factory method mechanism is exploited by the DI Container to load them. Sometimes dependencies are injected at the runtime by the choices made by user when the software is executed. This may be selecting from the Drop Down what Sorting Strategy we can use for the set of input arrays.
LINQ and Strategy Pattern

Lambda Expressions or Queries or LINQ operators are also the Strategies those specify the SQL Operations at higher level of abstraction. We pass the data set and operations that we want to perform on it and we get the results back.

Usually the Class has Family of behaviors within itself. All behaviors are defined as different methods within class, and another method has big switch statement, and some flag in this switch statement decides, what behaviors will be provided by the class. 

An object can have Family Of Behaviors, but only one at one time. This behavior can be injected while creating that object or it can be configured at run time.

For example, an object encapsulate an Array Sorting Algorithms, and it can be configured at object creation time or it can be configured at run time.

At run time, behavior can be added to an object, by Strategy Pattern. It is a composition based solution, delegate behavior to dynamically added objects. We have A, B and C type of behaviors and further we have A1, A2 and A3 type of behaviors. In the same type B1, B2 and B3. 

Both State and Strategy pattern provide composition and delegation based solution to provide plug-able behavior for the object. But do not confuse this with State Pattern, in which object changes its behavior depending upon its state. In Strategy pattern, some external stimulus cause the object to have any one of the available family of behaviors. This external behavior can be specified at object creation time by placing them in configuration files, in database tables, by GUI in which case it is specified by the User, by another object that is responsible to create the object, by events and delegates, by dependency injection. But in case of State Pattern, the internal state change cause the new behavior to be plugged in. So the setters methods of class plug-in the new behavior for the class. 

We define an Interface for the Family of Behaviors. For, all different behaviors we define class that extends interface. The behaviors is passed as object to the Subject Class at the time of object creation or set by setters at runtime, The behaviors class can also be specified in the configuration files or they are provided by the User at runtime by the GUI Control. 

No comments:

Post a Comment