Design pattern "State"
Real world example
The βstateβ design patterns works like a state machine (also known as a finite state machine). A state machine is defined by a list of its states, its initial state, and the conditions for each transition to go from one state to another. However, itβs important to note that the machine can only be in one of these states at any particular moment. It changes from one state to another in response to some external inputs.
Finite state machines are widely used in modeling of application behavior, design of hardware digital systems, software engineering, compilers, network protocols, and the study of computation and languages.
Letβs consider a real-world example of the state design pattern: an Order
processing system. In this system, an order can be in various states such as NewState
, ProcessedState
, ShippedState
, DeliveredState
, and CancelledState
.
We can draw our states like this:
Loading graph...
Using this pattern avoids using switch/case
or if/else
structure which can be a little tedious and hard to maintain.
In plain words
It lets you change the behavior of a class when the state changes.
Wikipedia definition
The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the patternβs superclass. The state pattern can be interpreted as a strategy pattern which is able to switch the current strategy through invocations of methods defined in the patternβs interface.
Programmatic example
First, letβs create all our states:
Then, letβs create our Order
class:
Letβs play with our code:
Diagram
Loading graph...
Some other examples
- Media Players: Media players often have different states like playing, paused, stopped, etc. The behavior of the player changes based on its state.
- Traffic Lights: A traffic light system can be modeled using the state pattern with states like red, yellow, and green, each having different behaviors.
- Authentication Systems: In an authentication system, a user can be in various states like authenticated, unauthenticated, or in the process of authentication.
- Game Development: In game development, a game character could have states like idle, running, jumping, etc. The characterβs behavior changes based on its current state.
- Document Management Systems: A document in a workflow might have states like draft, review, approved, published, etc. The operations that can be performed on the document depend on its state.
- E-commerce Systems: An order in an e-commerce system can have states like new, paid, shipped, delivered, etc. The transitions between these states can be managed using the state pattern.
- Networking: Network connections can have different states like established, listen, closed, etc. The behavior of the connection changes based on its state.
- Operating Systems: An operating system manages processes which can be in states like start, ready, running, waiting, etc.
- UI Development: In UI development, a component could have states like enabled, disabled, hover, active, etc. The appearance and behavior of the component change based on its state.
- Printers: A printer can have states like idle, printing, out of paper, out of ink, etc. The operations that can be performed on the printer depend on its state.
Design pattern "Strategy"
Let's learn what is the "Strategy" design pattern
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern π€ππΆπ§³
Design pattern "Iterator"
Let's learn what is the "Iterator" design pattern π
Design pattern "Observer"
Let's learn what is the "Observer" design pattern ππππ
Design pattern "Memento"
Let's learn what is the "Memento" design pattern πΈοΈπΌοΈπΌοΈππ°οΈπ
Design pattern "Facade"
Let's learn what is the "Facade" design pattern πΉοΈποΈπ¨
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern πͺβββ
Design pattern "Adapter"
Let's learn what is the "Adapter" design pattern π