• Albert Bennett

Design Patterns: State

Updated: Jul 12, 2021

Link to the GitHub repo :)

Feel free to like, comment and share.


For today's topic... the state pattern.

This pattern is useful for when you are trying to manage state in your project but, don't want to use a full blown state machine.


To better explain how the state pattern works here is a UML diagram of how each aspect of it works:


There are several parts to the State pattern. I'll be explaining the role of each of them.


Context: The job of the context object is to hold a reference to the current state of the client.


State: This is an abstract class that each state is going to inherit from. Also, it contains a reference to the context object such that states can transition between them.


StateA/ StateB: These are concrete implementations of the state object. Each one manages the transition of states.


State changing in action!



I hope that this help you to understand the State Pattern.


<< More Design Patterns

4 views0 comments

Recent Posts

See All