Search code examples
design-patternsstrategy-pattern

Where is the benefit in using the Strategy Pattern?


I've looked at this explanation on Wikipedia, specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example?


What I can make from the answers so far, it seems to me to be just a more complex way of doing this:

have an abstract class: MoveAlong with a virtual method: DoIt()
have class Car inherit from MoveAlong, 
     implementing DoIt() { ..start-car-and-drive..}
have class HorseCart inherit from MoveAlong, 
     implementing DoIt() { ..hit-horse..}
have class Bicycle inherit from MoveAlong, 
     implementing DoIt() { ..pedal..}
now I can call any function taking MoveAlong as parm 
passing any of the three classes and call DoIt
Isn't this what Strategy intents? (just simpler?)

[Edit-update] The function I refer to above is replaced with another class in which MoveAlong would be attribute which is set according to need based on the algorithm implemented in this new class. (Similar to what is demonstrated in the accepted answer.)


[Edit-update] Conclusion

The Strategy Pattern has it's uses, but I am a strong believer in KISS, and would tend to more straightforward and less obfuscatory techniques. Mostly since I want to pass on easily maintainable code (and 'cos I'll most likely be the one who have to make the changes!).


Solution

  • The point is to separate algorithms into classes that can be plugged in at runtime. For instance, let's say you have an application that includes a clock. There are many different ways that you can draw a clock, but for the most part the underlying functionality is the same. So you can create a clock display interface:

    class IClockDisplay
    {
        public:
           virtual void Display( int hour, int minute, int second ) = 0;
    };
    

    Then you have your Clock class that is hooked up to a timer and updates the clock display once per second. So you would have something like:

    class Clock
    {
       protected:
          IClockDisplay* mDisplay;
          int mHour;
          int mMinute;
          int mSecond;
    
       public:
          Clock( IClockDisplay* display )
          {
              mDisplay = display;
          }
    
          void Start(); // initiate the timer
    
          void OnTimer()
          {
             mDisplay->Display( mHour, mMinute, mSecond );
          }
    
          void ChangeDisplay( IClockDisplay* display )
          {
              mDisplay = display;
          }
    };
    

    Then at runtime you instantiate your clock with the proper display class. i.e. you could have ClockDisplayDigital, ClockDisplayAnalog, ClockDisplayMartian all implementing the IClockDisplay interface.

    So you can later add any type of new clock display by creating a new class without having to mess with your Clock class, and without having to override methods which can be messy to maintain and debug.