Let’s go through the problem first design to elevate. Let’s assume you are in-charge of task for designing set of Vehicles. To start off with,you will surely design a Vehicle abstract class which will support certain set of common characteristics like Change Gear, Type, Engine, Number of Gears,
Let’s extend Vehicle class to Street Racer vehicle
StreetRacerVehicle is called at a client and will invoke go() method.
Going further, business objectives changes and it started another production line of producing Formula1 cars, hence, you decided to implement Formula1 class by extending Vehicle class. Obviously, it will support the basic characteristics of the Vehicle.
Now, we have created two different types of objects here of type – StreetRacer named racer, and ForumlaOne named forumla1.
When executed, the output will be as below –
And, if you further extend the class for further vehicles viz. Helicopter, and instantiate the class at client and invoke go() method, you will get the same set of output. You see the problem has started now, as if you go on extend the Vehicle class, it will return the same output irrespective of how it is implemented or rather say, which Vehicle should be drove, or fly. The problem is you have classes that extends the abstract to the functionality agreed with the base class functionality. Solution could be override the go() method in each of the new classes and the output will be much different. Like –
However, you are now maintaining separate code for various generations of inheritance, and it will spread out the various generations of code when extended or inherited classes. This is where Strategy Patterns addresses the problem that you can write the code in base class without overriding the base class functionality in inherited classes or in all the multiple generations of classes.
Strategy Design Pattern says instead you should have code with algorithm variable. Variable is changed based on the series of objects that base class is derived to. That is, the object being is worked upon or object that has been derived to. The idea is to separate out algorithm code from the class into algorithm object and allow your algorithm object to be different and you use algorithm object that will let you supply different method that use different algorithms. Using the polymorphism technique, you can thus identify the object variables. Hence, this is different way of looking at Object way of programming and the relationship between the base and derived class will have a “has a”.
courtesy – wikipedia.org
Let’s write a Interface which will support the change in behviour of vehicles, but still supports common characteristics like go, Engine Start, Engine Stop, Change Gear, Has Gear, etc.
Now, implement the interface using different implemented class – like explained above, create a interface of common characteristics and implement it differently.
Let’s change the abstract class Vehicle to support the Strategy pattern.
What it has been done is we are storing the algorithm object as a member object to utilise and invoke the correct algorithm of type object. Hence, of any type vehicle can now support different behavior by setting the proper algorithm when initialized like for FormulaOne and StreetRacer it will be driving, for Helicopter – it will be flying and for Jet it will be fast flying. For setting different algorithm based on the derived class, we have to set inside the constructor of each derived class of Vehicle. Like –
When the solution is ran, following output is generated –