This repository demonstrates a clean implementation of the State design pattern in C++ for a console-based application.
IState.hpp– Interface for defining state behaviorStateA.hpp / StateA.cpp– Concrete state A implementationStateB.hpp / StateB.cpp– Concrete state B implementationContext.hpp / Context.cpp– Maintains current state and delegates behaviormain.cpp– Entry point showcasing dynamic state transitions
- Compiler: Tested with
MSVC v19.44(Visual Studio 2022) - C++ Standard: C++17 or higher
Context context;
context.setState(new StateA());
context.request(); // Executes StateA behavior
context.setState(new StateB());
context.request(); // Executes StateB behavior