UnderGround Forums
 

ITGround >> Lesson: Strategy Design Pattern


10/23/05 5:15 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 23-Oct-05
Member Since: 05/13/2002
Posts: 8637
 
The Strategy (GoF) pattern is basically: giving the ability to provide an algorithm (implementation) to another class so it can handle a specific operation. The best anology I have seen was in MSDN magazine when they talked about using IComparer. You've probably already used this pattern without know its formal name. Just so I can walk you through it "codified" I'll do something that MSDN magazine didn't do...show the code. If you take a look at the code below we have a Person class and a PersonCollection. The SortByName method on the PersonCollection class wraps an ArrayList.Sort() call. ArrayList.Sort lets you provide an IComparer implementation...or strategy. To be clear, I've derived my own class from IComperer called "Strategy". This contains a very simple strategy algorithm which is defaulting to use the String.Compare operation so we can sort alphabetically by name. The call to this.InnerList.Sort passes an instance of the Strategy class. Quite simply, this is the GoF strategy pattern.
public class Person {
    
//should be properties, but to be brief we'll use fields
    
public string Name;
    public int 
Age;

    public 
Person(string name, int age) {
        Name 
name;
        
Age age;
    
}
}

public class PersonCollection : CollectionBase {
    
class Strategy : IComparer {
        
public int Compare(object x, object y) {
            Person px 
as Person;
            
Person py as Person;
            return 
String.Compare(px.Name, py.Name);
        
}
    }

    
public void SortByName() {
        
this.InnerList.Sort(new Strategy());
    
}

    
public void Add(Person value) {
        
this.InnerList.Add(value);
    
}
}

class Program
{
    
static void Main(string[] args) {
        PersonCollection people 
= new PersonCollection();
        
people.Add(new Person("Bubba"20));
        
people.Add(new Person("Al"22));
        
people.Add(new Person("Cooter"99));
        
people.SortByName();

        
//prints: Al, Bubba, Cooter
        
foreach(Person p in people)
            Console.WriteLine(p.Name)
;

    
}
}
10/23/05 5:23 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 23-Oct-05
Member Since: 05/13/2002
Posts: 8638
Note that in C# 2.0, which will RTM in about 2 weeks, you can simplify the above example quite a bit using anonymous methods. It turns out the to sort a generic List<T>, you can pass in a System.Comparison delegate like so...

 

static void Main(string[] args) {

    List<Person> people 
= new List<Person>();
    
people.Add(new Person("Bubba"20));
    
people.Add(new Person("Al"22));
    
people.Add(new Person("Cooter"99));
    
    
//use anonymous method here...
    
people.Sort(delegate(Person x, Person y) {
                    
return String.Compare(x.Name, y.Name);
                
});
    

    
//prints: Al, Bubba, Cooter
    
foreach(Person p in people)
        Console.WriteLine(p.Name)
;

}
10/23/05 5:37 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 23-Oct-05 05:37 PM
Member Since: 05/13/2002
Posts: 8639
While we're on the subject, in C# 3.0 you can make this example even more simple using lambdas. The whole strategy pattern for sorting because a single lambda expression like so...

static void Main(string[] args) {

    List<Person> people 
= new List<Person>();
    
people.Add(new Person("Bubba"20));
    
people.Add(new Person("Al"22));
    
people.Add(new Person("Cooter"99));
    
    
//use the C# 3.0 lambda feature
    
people.Sort(((x, y) => String.Compare(x.Name, y.Name)))
;
    

    
//prints: Al, Bubba, Cooter
    
foreach(Person p in people)
        Console.WriteLine(p.Name)
;

}
11/20/05 3:56 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 20-Nov-05
Member Since: 05/13/2002
Posts: 8866
Is this too much for everyone??? :-)
11/20/05 7:46 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 20-Nov-05
Member Since: 05/13/2002
Posts: 8870
I've already mastered notepad, and for the last time...I'm not interested in animal porn or Joe Ray's anal videos. Sheesh.
1/27/06 9:23 PM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Edited: 27-Jan-06
Member Since: 05/13/2002
Posts: 9515
TTT by request...
1/28/06 12:16 AM
Ignore | Quote | Vote Down | Vote Up
Revolver of Reason
Send Private Message Add Comment To Profile

Edited: 28-Jan-06
Member Since: 01/01/2001
Posts: 29679
ttt for a good'un
11/13/12 11:37 AM
Ignore | Quote | Vote Down | Vote Up
theseanster
254 The total sum of your votes up and votes down Send Private Message Add Comment To Profile

Member Since: 5/13/02
Posts: 21201

bump to bring back from the dead


Reply Post

You must log in to post a reply. Click here to login.