The state pattern

The state pattern allows the change in the object's behavior based on the change of its state.

If the state is changing for an object, the behavior automatically changes as well. For example, a person whose state is nervous behaves strangely, while a person whose state is happy behaves in a more positive way.

The state pattern basically implements the state machine in a way that each state is a derived class of a state interface, and they alter between them jumping from one state to another, thus dynamically updating the behavior as per the state.

Let's jump to our example. We want to know the number of days in a season, either summer, winter, autumn, or spring. We will always invoke one single method to know the number ...

Get Enterprise Application Architecture with .NET Core 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.