We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
Strategy pattern:
The problem is solved by the strategy pattern.
If we have a base class that has some behaviors and some subclasses that inherit from it. In many situations, we can observe that we are confused about any subclass that should be inherited from another. We found some classes that are common in one behavior and different in another one. In this problem, we use a strategy pattern instead of just code and paste the same code in different places. The solution is that there is a class that has some behaviors and an interface for each behavior and this interface has different strategies or different implementations for the method. Then we create customed objects by passing the right behaviors and we can change the behavior of any object in run time by passing another strategy. In the strategy pattern, there are three components: 1- Context object that has the reference for all strategy objects. It is the interface or abstract class that is inherited from it. 2- Strategy objects represent the different algorithms or behaviors the context object can use.
3- Runner class: the class receives the behavior and executes
it.
In the strategy pattern, we don’t need to hard code inside the
class instead of that we can implement different behavior or strategy objects and use them. That gives us the flexibility to grow horizontally with more strategy objects without needing to change the code of the runner class.