1
edit
Changes
Strategy
,no edit summary
==Defenition==
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.
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
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>