Home  /  Questions  /  Question



250   96.9
Mar 06, 2011


Strategy Design Pattern usage in .NET

I have a quick question: How is the Stategy Pattern used in .NET?



370   99.9
Mar 06, 2011
Hi,

for example when you implement IComparable interface, you define the strategy for comparing which may differ per implementation, but the contract remains the same. You may replace the implementation without affecting the client. The same could be with ICloneable etc.

Hope this helps,
Robert 
 1 comment
 
thanx a lot --- Rajeev Kumar  Mar 08, 2011

1,364   100.0
Mar 06, 2011
I have a brief blog post here..

http://30minutestotechnirvana.blogspot.com/2007/12/session-1.html


In a nutshell.. (extracted)
Applications of Strategy pattern in .NET Framework

(1) Array and ArrayList provides capability to sort object. By default they use QuickSort algorithm. The Sort method will use the IComparable implementation for each element to handle the comparisons necessary for sorrting. To change the sortig strategy use the overload of Sort that takes an IComparer as a parameter.

ICompararer.Compare is then used for the comparisons.

The new List 
makes heavy use of the strategy pattern. In addition to the updated Sort method, the find-related methods, BinarySearch and others all take parameters that allow parts of the respective algorithms to vary based on the needs of the caller. The use of Predicatedelegate in the FindAll method lets the caller use any method as a filter for the List so long as it takes the appropriate object type and returns a boolean.

Hope this helps.
 1 comment
 
thanx a lot --- Rajeev Kumar  Mar 08, 2011

1,128   99.9
Mar 08, 2011
The Strategy Pattern has been discussed earlier in this forum (see this thread -> http://dofactory.com/topic/1158/replace-conditional-vb-net-select-case-with-strategy-pattern-how.aspx), which might be of help to you.

Also, if you read the comments I made to the above post, you can see an implementation where enums and conditionals are not needed. Do note, that these are some ways to implement the Strategy Pattern in .Net. I have seen quite a few different implementations. But this should get you started.


Good Luck!

 1 comment
 
thanx a lot --- Rajeev Kumar  Mar 08, 2011