Design Pattern — State Pattern ( C++ example)

Chandan S P
Jun 26, 2021

Design Pattern is a solution to solve the recurring problem in software engineering.

A State Diagram is a way of representation of system behavior in terms of diagram and State pattern is a solution to implement a state diagram where it allows to change the behavior of the object based on internal states.

State Diagram can also be implemented using if-else or switch case but it will be difficult to manage when the system grows

Consider an example System which manages the Lock and unlock of a Car door using a wireless key

Here is a state machine considering only major use cases , the Car door handling system has 2 states it will land into DoorOpen and DoorClosed based on the Key press of the wireless device

Class Diagram for the above system

Below is the implementation in C++

--

--