Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Strategy Pattern work? [closed]

How does it work, what is it used for and when should one use it?

like image 761
Jorge Córdoba Avatar asked Sep 18 '08 12:09

Jorge Córdoba


People also ask

How does the strategy pattern help satisfy the Open Closed Principle?

The strategy pattern has to do with selecting different algorithms at run time. It doesn't say anything about whether or how a class should be modified during maintenance. The open/closed principle has to do with allowing extensions during maintenance, but not modifications.

How do strategy patterns work?

In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

Which design patterns use Open Closed Principle?

The Open Closed Principle: Software entities should be open to extension but closed to modification. The Liskov Substitution Principle: Derived classes must be substitutable for their base classes. The Interface Segregation Principle: Make fine-grained interfaces that are client specific.

What problem does the strategy pattern solve?

The strategy pattern is used to solve problems that might (or is foreseen they might) be implemented or solved by different strategies and that possess a clearly defined interface for such cases.


2 Answers

Let's explain the strategy pattern the easy way:

You have a class Car() with a method run(), so you use it this way in a pseudo language:

mycar = new Car() mycar.run() 

Now, you may want to change the run() behavior on the fly, while the program is executing. For example, you might want to simulate a motor failure or the use of a "boost" button in a video game.

There are several ways to do this simulation: using conditional statements and a flag variable is one way. The strategy pattern is another: it delegates the behavior of the run() method to another class:

Class Car() {     this.motor = new Motor(this)       // passing "this" is important for the motor so it knows what it is running      method run()     {         this.motor.run()     }      method changeMotor(motor)     {         this.motor = motor      }  } 

If you want to change the car's behavior, you can just change the motor. (Easier in a program than in real life, right? ;-) )

It's very useful if you have a lot of complex states: you can change and maintain them much more easily.

like image 94
e-satis Avatar answered Oct 17 '22 01:10

e-satis


Problem

The strategy pattern is used to solve problems that might (or is foreseen they might) be implemented or solved by different strategies and that possess a clearly defined interface for such cases. Each strategy is perfectly valid on its own with some of the strategies being preferable in certain situations that allow the application to switch between them during runtime.

Code Example

namespace StrategyPatterns {   // Interface definition for a Sort algorithm   public interface ISort   {     void Sort(List<string> list)   }    // QuickSort implementation   public class CQuickSorter : ISort   {     void Sort(List<string> list)     {       // Here will be the actual implementation     }   }    // BubbleSort implementation   public class CBubbleSort : ISort   {     void Sort(List<string> list)     {       // The actual implementation of the sort     }   }    // MergeSort implementation   public class CMergeSort : ISort   {     void Sort(List<string> list)     {       // Again the real implementation comes here     }   }    public class Context   {     private ISort sorter;      public Context(ISort sorter)     {       // We pass to the context the strategy to use       this.sorter = sorter;     }      public ISort Sorter     {       get{return sorter;)     }   }    public class MainClass   {     static void Main()     {        List<string> myList = new List<string>();         myList.Add("Hello world");        myList.Add("Another item");        myList.Add("Item item");         Context cn = new Context(new CQuickSorter());        // Sort using the QuickSort strategy        cn.Sorter.Sort(myList);        myList.Add("This one goes for the mergesort");        cn = new Context(new CMergeSort());        // Sort using the merge sort strategy        cn.Sorter.Sort(myList);     }   } } 
like image 35
Jorge Córdoba Avatar answered Oct 17 '22 02:10

Jorge Córdoba