Open main menu

CDOT Wiki β

Changes

Strategy

690 bytes added, 17:14, 7 February 2007
no edit summary
#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.<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>
class CoolComparer : 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();
items.Sort(myComparer); // Delegate Comparison Method
1
edit