Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

Patterns in .NET: Citing the Strategy pattern

Strategy is a quite frequently used pattern. Strategy pattern involves "defining a family of algorithms, encapsulating each one, and making them interchangeable. Strategy lets the algorithm vary independently from clients that use it."

Essentially strategy entails creating an abstract class or an interface abstracting the family of algorithms to be used. I must say that there are umpteen applications of this pattern within the BCL. Let's examine one of them here:

IComparer is an interface defined in the System.Collections namespace and defines a method that can compare two objects. It's primary purpose is to provide a way to customize the sort order of a collection, which is required in the Array.Sort and Array.BinarySearch method implementations. So mapping this to the Strategy (refer to the link above for the class diagram of the pattern):

Icomparer Interface : Strategy, Array class : Context

On similar lines, another citation would be IListSource / IList, which is used in data binding in Webforms/Windows forms.

I will leave it as an exercise to the adept .NET Fx developer to just add plenty more to this list!

 

Posted: Apr 04 2005, 03:17 PM by Manoj G | with 2 comment(s)
Filed under:

Comments

Manoj G said:

Another example of Strategy within the BCL:

http://blogs.ugidotnet.org/adrian/archive/2004/05/04/1353.aspx

In this category of posts on my blog (http://blogs.ugidotnet.org/adrian/category/309.aspx?Show=All) you can find some other patterns implemented within the BCL
# April 30, 2005 7:05 PM

Manoj G said:

Hi,

sorry but I think I am bit too slow to understand the advantages of this pattern.

What do you get by using the strategy pattern instead of defining an interface and a bunch of classes implementing the interface?

Thanks.
# July 29, 2005 4:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)