Strategy Pattern


Yesterday, One of my coworkers presented an overview of the Strategy Pattern.  There is already a lot about this (and other) patterns online, but I thought it might be useful (to me) to put up a “schematic” of the pattern for future reference:

interface IStrategicInterface
{
     StrategicMethod(parms);
}

class SuperClass : IStrategicInterface
{
     StrategicMethod strategicMemberFunction;
}

class SubClassA : Superclass or IStrategicInterface
{
    public SubClassA(IStrategicInterface conretedStrategicMethod)
    {
        strategicMemberFunction = concretedStategicMethod;
    }
}

class SubClassB : Superclass or IStrategicInterface
{
    public SubClassB(IStrategicInterface conretedStrategicMethod)
    {
        strategicMemberFunction = concretedStategicMethod;
    }
}

class ConcreteStrategy1 : IStrategicInterface
{
      StrategicMethod(parms) {...1...}
}

class ConcreteStrategy2 : IStrategicInterface
{
      StrategicMethod(parms) {...2...}
}

Now objects of either subclass (SubClassA or SubClassB) can be constructed to accept either ConcreteStategy1 or ConcreteStrategy2 as the method they will use when the strategicMemberFunction() is called.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s