State Pattern

The State Pattern is a behavioural design pattern that allows an object to change its behaviour when its internal state changes.
The only way to change the state of an object is through transitions.

source: https://www.codiwan.com/state-design-pattern-real-world-example-java

When to use the State Pattern?

  • ideal for models with state or statuses
  • pre-defined set of business requirements when transitioning from one state to another
  • e.g. users/status: pending, confirmed, blocked
  • e.g. orders/status: draft, pending, paid, dispatched, delivered
  • e.g. posts/status: draft, submitted, approved, published