The State Pattern allows an object to alter its behavior when its internal state changes. It achieves this by defining an interface (or abstract base class) for various states and concrete classes for each specific state. The context object holds a reference to the current state and delegates behavior based on that state.

#include <iostream>
#include <memory>
class IState {
public:
virtual ~IState() {}
virtual void insertMoney(float amount) = 0;
virtual void selectItem(int index) = 0;
virtual float getMoneyBack() = 0;
};
class VendingMachine {
private:
std::unique_ptr<IState> currentState;
public:
VendingMachine() : currentState(std::make_unique<StateIdle>(*this)) {}
void setState(std::unique_ptr<IState> state) { currentState = std::move(state); }
void insertMoney(float amount) { currentState->insertMoney(amount); }
void selectItem(int index) { currentState->selectItem(index); }
float getMoneyBack() { return currentState->getMoneyBack(); }
};
class StateIdle : public IState {
VendingMachine& machine;
public:
StateIdle(VendingMachine& m) : machine(m) {}
void insertMoney(float amount) override {
std::cout << "Money inserted: " << amount << std::endl;
machine.setState(std::make_unique<StateMoneyInserted>(machine));
}
void selectItem(int index) override {
std::cout << "Insert money first!" << std::endl;
}
float getMoneyBack() override {
return 0.0f; // No money to return in Idle state
}
};
class StateMoneyInserted : public IState {
VendingMachine& machine;
public:
StateMoneyInserted(VendingMachine& m) : machine(m) {}
void insertMoney(float amount) override {
std::cout << "Money already inserted." << std::endl;
}
void selectItem(int index) override {
// Simulate successful item selection
std::cout << "Selected item " << index << std::endl;
machine.setState(std::make_unique<StateIdle>(machine));
}
float getMoneyBack() override {
// Simulate returning all money
float moneyBack = 10.0f; // Replace with actual logic for vending machine
std::cout << "Returning money: " << moneyBack << std::endl;
return moneyBack;
}
};
int main() {
VendingMachine vm;
vm.insertMoney(5.0f);
vm.selectItem(1);
vm.getMoneyBack();
return 0;
}
Problems to solve with State Design Pattern
- Traffic Light Controller:
- Problem: A traffic light needs to change its color (red, yellow, green) based on a timer and external events (e.g., pedestrian button press). The code becomes messy with numerous conditional statements depending on the current light color and timer values.
- Media Player:
- A media player can be in various states (playing, paused, stopped) and needs to respond to user actions (play, pause, stop) differently depending on the current state. Conditional logic within the player class can become intricate as more features (e.g., forward, rewind) are added.
- Vending Machine
- Expand on the basic vending machine example to include additional states like
DispensingProductandOutOfStock. This state can handle product dispensing logic and manage transitions based on product availability.
- Expand on the basic vending machine example to include additional states like
Follow more posts @ https://jdecodes.wordpress.com
My all design pattern articles :
- https://jdecodes.wordpress.com/2024/07/13/builder-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/command-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/iterator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/13/mediator-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/state-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/memento-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/observer-design-pattern/
- https://jdecodes.wordpress.com/2024/07/19/strategy-pattern/
- https://jdecodes.wordpress.com/2024/07/20/visitor-design-pattern/
- https://jdecodes.wordpress.com/2024/07/20/adapter-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/20/bridge-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/composite-desing-pattern/
- https://jdecodes.wordpress.com/2024/07/22/facade-design-pattern/
- https://jdecodes.wordpress.com/2024/07/22/decorater-design-pattern/
Leave a comment