Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and State design is also one of the solution so we need to find the problem that State 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++ State Design Pattern :
Standard Definition:
It defines that allow an object to alter(change) its behaviour when its internal state changes, the object will appear to change its class.
In layman language:
Object Oriented State machine (state machine with classes and each state is represented by individual class).
Problem:
State machine scenarios, How to handle state machine with object oriented principle (with the help of classes).
Solution:
Create one class named as context, it will have pointer of another class named as state (this is base class) and each derived class will represent specific state. context object will behave same like state machine (depend upon the object of state it has) , this solution called as “state design pattern“.
When we will use state design pattern:
- When we have many states that needs to be handled.
- when we want to handle each state in separate class (we are processing the data separately).
Advantage:
- Loose coupling (each state is represented in separate class so we can handle the different sate in different class).
- Flexible (We can add new state at any time without polluting existing code).
Key Points:
- There is state class and its derive class represent concrete state.
- There is context class having state class pointer.
- Transition of state can be handled at two places : in context class or each derive class.
- State class mostly created as singleton class.
- This pattern structure similar to bridge pattern but in bridge there is separate hierarchy of context also.
Class Diagram:
Class Diagram of real example:
Source code with real example:
/* Program: State Design Pattern Author: Alpha Master Date: 4 Nov 2021 */ //Header File #include<iostream> #include<thread> //state class class TLState { public: virtual void HandleReq() = 0; }; class TLRed: public TLState { public: static TLRed* GetInstance() { static TLRed m_pRed; return &m_pRed; } virtual void HandleReq() { std::cout<<"Red is glowing for 1 sec"<<std::endl; } }; class TLYellow:public TLState { public: static TLYellow* GetInstance() { static TLYellow m_pinstance; return &m_pinstance; } void HandleReq() { std::cout<<"Yellow is glowing for 1 sec"<<std::endl; } }; class TLGreen: public TLState { public: static TLGreen* GetInstance() { static TLGreen m_pGreen; return &m_pGreen; } void HandleReq() { std::cout<<"Green is glowing for 1 sec"<<std::endl; } }; class TLController { TLState* mtlstate; public: TLController() { mtlstate = nullptr; } void run() { int i =0; while(i< 3) { mtlstate = TLRed::GetInstance(); mtlstate->HandleReq(); std::this_thread::sleep_for(std::chrono::seconds(1)); mtlstate = TLYellow::GetInstance(); mtlstate->HandleReq(); std::this_thread::sleep_for(std::chrono::seconds(1)); mtlstate = TLGreen::GetInstance(); mtlstate->HandleReq(); std::this_thread::sleep_for(std::chrono::seconds(1)); i++; } } }; int main() { std::cout<<"State Design Pattern"<<std::endl; //start TLController obj; obj.run(); return 0; }
Output:
State Design Pattern Red is glowing for 1 sec Yellow is glowing for 1 sec Green is glowing for 1 sec Red is glowing for 1 sec Yellow is glowing for 1 sec Green is glowing for 1 sec Red is glowing for 1 sec Yellow is glowing for 1 sec Green is glowing for 1 sec