How to implement State Design Pattern in C++
Posted by TheEyebal@reddit | learnprogramming | View on Reddit | 4 comments
I am learning state design pattern and I am struggling with implementing it. I read the Design Patterns: Elements of Reusable Object-Oriented Software and understood the concept
I am also reading this to understand state design patterns.
I've also watched a number of different youtube videos, drawn it out on a whiteboard, and scrolling on reddit, wikipedia, and stack overflow to understand it but so far I am lost
I have resorted to asking ChatGPT for help (which I hate) but also still confused
What I understand from this site is there is a context class that delgates behavior of the object to the states
I had wrote this thinking that this was a context class but according to ChatGPT it is not
#include <iostream>
using namespace std;
// a class holding an enum, not a true state pattern implementation.
enum controlSwitch { // List out the states
ON = 1,
OFF = 0
};
class Light { // Context class
public:
enum controlSwitch bulb; // create an enum variable
Light(controlSwitch state){ // Constructor
bulb = state; // enum variable = state parameter
}
};
int main(){
Light myLight(OFF); // create class variable
cout << myLight.bulb << endl; // reads the value of the enum
return 0;
}
Apparently I am suppose to have a context class move to the next state. So I would need a ON class and OFF class.
what is the pre requisite
I am at a lose, can someone help with this
sophieximc@reddit
State pattern in C++ usually clicks once you stop thinking of it as switches and more like swapping behavior objects.
I ended up doing it with a base State interface and concrete states that handle the same methods differently. Then your context just holds a pointer to the current state and delegates calls.
The slightly annoying part in C++ is ownership and transitions, like who creates the next state, but smart pointers make it less messy.
Once you get a simple example working it kinda feels obvious after that.
TheEyebal@reddit (OP)
Alright thanks
I am also starting to realize that I need to learn what interfaces are which I am watching a tutorial on to understand
Like there are prerequisites I need to learn
HashDefTrueFalse@reddit
So this defines possible states and stores the current state of a light instance, but there's no state transitions or any way to make that happen. It's really up to you how you do that. You can add a method to Light (e.g. toggleState) that decides which state to transition to based on the current state plus any supplemental data you want to use.
I wrote someone a traffic light state machine example (coincidentally also in C++) a while ago. This one uses a lookup table defined at compile-time to map the current state to the next. It's a very simple data-driven way to achieve this, but you can use if/switch statements to decide where to transition at runtime too.
Brrrrrrrr.
aqua_regis@reddit
You are close, but not close enough.
The following is for Java, but it's similar enough to C++ that you could understand it: https://java-design-patterns.com/patterns/state/
For the state pattern you'd have two states - on and off - these would be in individual classes.
You'd have a trigger that changes the state - this would be a
LightSwitch- an actor that acts upon the bulb - even this LightSwitch would have the two states.The state of the LightBulb is the result of the state of the LightSwitch - so, actually, the LightBulb would observe (you could implement the Observer pattern here) the LightSwitch and toggle its state depending on what is observed.