Dofactory.com
Dofactory.com
 Back to list
Views:   19.6K
Replies:  3
Archived

Strategy Design Pattern usage in .NET

I have a quick question: How is the Stategy Pattern used in .NET?
Rajeev Kumar, Mar 06, 2011
Reply 1
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!

Robert Blixt, Mar 08, 2011
Reply 2
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.
Rajesh Pillai, Mar 06, 2011
Reply 3
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 
Robert Varga, Mar 06, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.