Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and strategy is also one of the solution so we need to find the problem that strategy has solved and how ?
This design pattern comes under Behavioral Category (It tells that how objects are communicating with each other (objects have been created already)).
================================================================
C++ Strategy design pattern:
Standard Definition:
Strategy Design Pattern define a family of algorithms, encapsulate each one, and make then interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Technical Explanation:
Strategy Pattern says that (I will break down the above standard definition) :
- define a family of algorithms : we have many algorithms of similar or relevant type that we need to process one at a time, algorithm is used to handle input or anything that we need to process. (it is not related to sorting or search algorithm) .
- encapsulate each one : we have separate class to handle each algorithm (input).
- make then interchangeable : during run time, client can request to move or shift to different algorithm.
- Strategy lets the algorithm vary independently from clients that use it : Each algorithm can be changed or enhanced without effecting client code.
In layman language:
We need to handle different scenario at run time. Its mostly used in MVC pattern, where its placed inside controller to handle different inputs of user.
Problem:
How to handle different behaviour (inputs) and possibility of switching behaviour during run time.
Solution:
Keep each algorithm (input) encapsulate in separate class and process it separating, these algorithm can be interchangeable during run time on need basis (if user wants to switch to different behaviour during run time), we can add new algorithm (new behaviour) by just adding new derived class, this solution called as “strategy design pattern“.
When we will use strategy pattern:
- When we have many related class and each represent specific behaviour
- When user wants to do switch behaviour during run time.
- When there is probability of new similar behaviour can come in future.
Class Diagram:
Class Diagram of real example:
Source Code of real example in C++:
/* * File:strategy.cpp * This file is described the strategy Design Pattern with help of example * Strategy pattern is structutal Pattern * Author: Aplha Master * Date: 31 Dec 2020 */ //Header File #include<iostream> //TreadMill Class (Base class of different strategy) class TreadMillStrategy { protected: int m_speed; public: virtual void Start() = 0; virtual void Stop() = 0; }; class ChildStrategy:public TreadMillStrategy { public: ChildStrategy(int speed){ m_speed = speed;} ~ChildStrategy(){} void Start() { if(SpeedRestriction()) { std::cout<<"Child TreadMill Mode Started, speed:"<<m_speed<<std::endl; //Call the Driver api to start the Motor } else { std::cout<<"Child TreadMill Mode Cannot start, speed is more"<<std::endl; } } void Stop() {std::cout<<"Child TreadMill Mode Stop"<<std::endl;} private: bool SpeedRestriction() { return (m_speed < 10) ? true : false;} }; //Adult TreadMill Mode class AdultStrategy:public TreadMillStrategy { public: AdultStrategy(int speed){ m_speed = speed; } ~AdultStrategy(){} void Start() { std::cout<<"Adult TreadMill Mode Started, speed:"<<m_speed<<std::endl; IncreaseSpeed(); std::cout<<"Adult TreadMill Mode speed increased, speed:"<<m_speed<<std::endl; } void Stop() {std::cout<<"Adult TreadMill Mode Stop"<<std::endl;} private: void IncreaseSpeed() { m_speed = m_speed + 10;} }; //Controller class Controller { TreadMillStrategy* m_st; public: Controller(TreadMillStrategy* tm): m_st(tm){} ~Controller(){} void SetStrategy(TreadMillStrategy* tr) { m_st = tr; std::cout<<"*************Changing Strategy at run tim*************"<<std::endl; } void StartInterface(){m_st->Start();} void StopInterface(){m_st->Stop();} }; //CLient int main() { // Strategy Pattern is Structutal Pattern so // Inside Controller we will not create of TreaMillStrategy // Below creation of different strategy can be done by Factory Pattern // Creation of different strategy is depend upon Client(HOw is using strategy) std::cout<<"Strategy Design Pattern"<<std::endl; TreadMillStrategy* tm = new ChildStrategy(5); Controller* con = new Controller(tm); con->StartInterface(); con->StopInterface(); ///////////////////////Change Strategy at run time; delete tm; tm = new AdultStrategy(10); con->SetStrategy(tm); con->StartInterface(); con->StopInterface(); delete tm; delete con; return 0; }
Compilation Command:
g++ -std=c++11 strategy.cpp
Output:
Strategy Design Pattern
Child TreadMill Mode Started, speed:5
Child TreadMill Mode Stop
*************Changing Strategy at run tim*************
Adult TreadMill Mode Started, speed:10
Adult TreadMill Mode speed increased, speed:20
Adult TreadMill Mode Stop