FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/State_pattern

State pattern

The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines. The state pattern can be interpreted as a strategy pattern, which is able to switch a strategy through invocations of methods defined in the pattern's interface.

The state pattern is used in computer programming to encapsulate varying behavior for the same object, based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to conditional statements and thus improve maintainability.

Switching to LOW speed.\");\n fan.setState(std::make_unique<LowSpeedState>());\n }\n};\n\nclass LowSpeedState : public FanState {\npublic:\n void handleButtonPress(Fan& fan) const override {\n std::println(\"Fan is on LOW speed -> Switching to HIGH speed.\");\n fan.setState(std::make_unique<HighSpeedState>());\n }\n};\n\nclass HighSpeedState : public FanState {\npublic:\n void handleButtonPress(Fan& fan) const override {\n std::println(\"Fan is on HIGH speed -> Turning OFF.\");\n fan.setState(std::make_unique<OffState>());\n }\n};\n\nint main() {\n Fan fan(std::unique_ptr<OffState>());\n\n // Simulate pressing the button several times\n fan.pressButton(); // OFF -> LOW\n fan.pressButton(); // LOW -> HIGH\n fan.pressButton(); // HIGH -> OFF\n fan.pressButton(); // OFF -> LOW\n\n return 0;\n}\n"}}'>importstd; usingstd::unique_ptr; // Abstract State classFanState{ public: virtualvoidhandleButtonPress(classFan&fan)const=0; virtual~FanState()=default; }; // Context classFan{ private: unique_ptr<FanState>currentState; public: explicitFan(unique_ptr<FanState>state): currentState{state}{} voidsetState(unique_ptr<FanState>state)noexcept{ currentState=state; } voidpressButton()noexcept{ currentState->handleButtonPress(*this); } }; // Concrete States classOffState:publicFanState{ public: voidhandleButtonPress(Fan&fan)constoverride{ std::println("Fan is OFF -> Switching to LOW speed."); fan.setState(std::make_unique<LowSpeedState>()); } }; classLowSpeedState:publicFanState{ public: voidhandleButtonPress(Fan&fan)constoverride{ std::println("Fan is on LOW speed -> Switching to HIGH speed."); fan.setState(std::make_unique<HighSpeedState>()); } }; classHighSpeedState:publicFanState{ public: voidhandleButtonPress(Fan&fan)constoverride{ std::println("Fan is on HIGH speed -> Turning OFF."); fan.setState(std::make_unique<OffState>()); } }; intmain(){ Fanfan(std::unique_ptr<OffState>()); // Simulate pressing the button several times fan.pressButton();// OFF -> LOW fan.pressButton();// LOW -> HIGH fan.pressButton();// HIGH -> OFF fan.pressButton();// OFF -> LOW return0; }

See also

edit

References

edit
Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 305ff. ISBN 0-201-63361-2.
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home