Difference between revisions of "Strategy"
(→Intoduction) |
|||
(17 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
− | == | + | ==Defenition== |
− | + | Allows you to define a set of algorithms, to be used by different objects. | |
+ | Strategy lets the algorithm vary independently from objects that use it. | ||
− | ==Diagram== | + | Objects can be assigned to algorithms on runtime. |
− | == | + | The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them. |
+ | |||
+ | |||
+ | ==UML Diagram== | ||
+ | [[Image:strategy.gif]] | ||
+ | |||
+ | ==Benefits and Drawbacks of using Strategy Pattern== | ||
+ | '''Benefits in using Strategy Pattern''' | ||
+ | |||
+ | #A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture. | ||
+ | #By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced. | ||
+ | #The application can switch strategies at run-time. | ||
+ | #Strategy enables the clients to choose the required algorithm, without using a "switch" statement or a series of "if-else" statements. | ||
+ | #Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class. | ||
+ | #Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically. | ||
+ | #The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations. | ||
+ | |||
+ | '''Drawbacks in using Strategy Pattern''' | ||
+ | |||
+ | #The application must be aware of all the strategies to select the right one for the right situation. | ||
+ | #Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies. | ||
+ | #Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement. | ||
+ | #In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one. | ||
+ | #Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact. | ||
==Code Sample== | ==Code Sample== | ||
− | === | + | Strategy Method in the .NET Framework that works with ArryList.<br> |
− | == | + | By default the QuickSort algorithm is used to sort item in the list. |
+ | Some times there is a need to use a different sort algorithm there is a overload of sort that takes an IComparer. | ||
+ | <br> | ||
+ | |||
+ | <br><pre> | ||
+ | class CoolComparer : IComparer | ||
+ | { | ||
+ | #region IComparer Members | ||
+ | |||
+ | public int Compare(object x, object y) | ||
+ | { | ||
+ | // TODO: implementation | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | #endregion | ||
+ | |||
+ | } | ||
+ | |||
+ | class SlowComparer: IComparer | ||
+ | { | ||
+ | #region IComparer Members | ||
+ | |||
+ | public int Compare(object x, object y) | ||
+ | { | ||
+ | // TODO: implementation | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | #endregion | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | ArrayList items = new ArrayList(); | ||
+ | |||
+ | items.Add("One"); | ||
+ | items.Add("Two"); | ||
+ | items.Add("Three"); | ||
+ | |||
+ | items.Sort(); // Uses IComparable on string object | ||
+ | |||
+ | IComparer myComparer = new CoolComparer(); | ||
+ | IComparer myComparer = new SlowComparer(); | ||
+ | items.Sort(myComparer); // Delegate Comparison Method | ||
+ | </pre> | ||
+ | |||
+ | ==References== | ||
+ | http://msdn2.microsoft.com/en-us/library/system.collections.comparer(VS.80).aspx<br> | ||
+ | http://www.codeproject.com/cpp/strategy.asp<br> | ||
+ | http://en.wikipedia.org/wiki/Strategy_pattern<br> |
Latest revision as of 12:34, 17 April 2007
Contents
Defenition
Allows you to define a set of algorithms, to be used by different objects. Strategy lets the algorithm vary independently from objects that use it.
Objects can be assigned to algorithms on runtime. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.
UML Diagram
Benefits and Drawbacks of using Strategy Pattern
Benefits in using Strategy Pattern
- A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.
- By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.
- The application can switch strategies at run-time.
- Strategy enables the clients to choose the required algorithm, without using a "switch" statement or a series of "if-else" statements.
- Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.
- Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.
- The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.
Drawbacks in using Strategy Pattern
- The application must be aware of all the strategies to select the right one for the right situation.
- Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.
- Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.
- In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.
- Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.
Code Sample
Strategy Method in the .NET Framework that works with ArryList.
By default the QuickSort algorithm is used to sort item in the list.
Some times there is a need to use a different sort algorithm there is a overload of sort that takes an IComparer.
class CoolComparer : IComparer { #region IComparer Members public int Compare(object x, object y) { // TODO: implementation return 0; } #endregion } class SlowComparer: IComparer { #region IComparer Members public int Compare(object x, object y) { // TODO: implementation return 0; } #endregion } ArrayList items = new ArrayList(); items.Add("One"); items.Add("Two"); items.Add("Three"); items.Sort(); // Uses IComparable on string object IComparer myComparer = new CoolComparer(); IComparer myComparer = new SlowComparer(); items.Sort(myComparer); // Delegate Comparison Method
References
http://msdn2.microsoft.com/en-us/library/system.collections.comparer(VS.80).aspx
http://www.codeproject.com/cpp/strategy.asp
http://en.wikipedia.org/wiki/Strategy_pattern