How to use the state design pattern in .Net

Take advantage of the state design pattern to allow an object to change its behavior when its internal state changes

Become An Insider

Sign up now and get FREE access to hundreds of Insider articles, guides, reviews, interviews, blogs, and other premium content. Learn more.

Design patterns are used to solve common design problems and reduce the complexities in our source code. The state design pattern is a behavioral pattern that is used to represent a one to many relationship between objects in such a way that if one of the objects is modified, then all of the dependent objects are also modified.

The Gang of Four defines the state design pattern as follows:

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

This article presents a discussion of how we can work with the state design pattern in .Net.

When to use the state design pattern

The state design pattern is typically needed when you have an object in your application that might pass through various phases. As an example, you might have an application that keeps track of the status of notifications that are sent via emails. At any given time, the status of such notifications could be any of five states: processed, delivered, opened, bounced, or undelivered.

To continue reading this article register now