The state design pattern

The state design pattern is really similar to the strategy design pattern that we looked at in the previous chapter.

The purpose of the state design pattern is to allow us to choose a different behavior of an object based on the object's internal state.

Basically, the difference between the state design pattern and the strategy design pattern comes from the following two points:

  • The strategy design pattern is about how an action is performed. It is usually an algorithm that produces the same results as other algorithms.
  • The state design pattern is about what action is performed. Depending on the state, an object could be doing different things.

Implementing the state design pattern also closely resembles the implementation ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.