Design patterns: State

Today, about a fairly simple pattern called State, which serves to the very simple operation, as the name says to change the behavior of the object, when its internal state changes, i.e. when some event will happens, more accurately I will explain further in the article 🙂

 

Discussion

In the simplest terms, the state pattern is that if the internal state of the object changes (we can rely on one of the variables of this object, eg on the bool type) from truth to false then we change the behavior of this object.

The most important elements in implementing this pattern are:

  • The Context class, which stores the current state of an object, overwrites this state and changes it.
  • An abstract State class that has defined abstract methods to change the state of an object.
  • The ConcreteState class, which inherits from the State class, implementing its methods which allow to change the state of the object.

The State pattern is used wherever there are any events, or the conditions to perform, sometimes is better to use the State pattern when we have a lot of complicated conditional statements then it is better to replace them with the State pattern. So in short, the State pattern can be used wherever there is complicated conditional logic. For example, we write an article as in wordpress and we have states of the Draft, Published article and so on, here you can use the State pattern.

 

Intent

  • Changing the behavior of an object when its internal state changes.
  • Minimizing the complexity of conditional statements.

 

Problem

You have to change the behavior of the object during the life of the so-called runtime program or you may have a set of quite complicated conditional statements, then a better solution will be to use the State pattern than several dozen conditional statements.

 

Use when:

  • In large projects, you have to change the behavior of the object depending on its state (In small and medium-sized projects using the State pattern does not make sense, it only adds unnecessary complicated logic).
  • You have a set of complicated conditional instructions.

 

Structure

Standard UML diagram of the State pattern:

You already know what these classes are doing. StateOne, StateTwo and StateThree are of course the ConcreteState classes,which inherit from the State abstract class.

It is worth mentioning that the client operates with specific classes only from the level of the Context class.

 

Examples

Diagram of the State pattern in code

And we already know that in this piece of the article we are going to the real work 🙂

I will show in this example what is going on in the State pattern. Let’s start with the Context class.

We overwrite the state of the object with this class, store it and change it using the Request() method, passing the Context class to the specific class inheriting from the State abstract class using this method.

Let’s see what the State class looks like and the specific classes that inherit it.

First the State class.

There is nothing to understand in this class, we just defined the goNext() method to implement it in the specific StateOne, StateTwo and StateThree classes that inherit from the State class. And in order to not use a specific type class in the Context class.

Let’s see the specific classes inheriting from the State class.

First, the StateOne class.

StateTwo

StateThree

We see that in each class with each call to the goNext() method, we change the state of the object in the Context class to another.

And of course, the client.

In the client, we only create the Context object and set the initial state of the object further, only we change the state of this object using the Request() method.

Result:

 

Bell states

Let’s do one more simple example with eg bell states.

Let’s start with the Context class, in this case Bell.

Now the State class, in this case the BellState class.

And specific classes inheriting from the BellState class, i.e. the BellRingingState class first.

And class BellSilentState.

And finally the customer.

Result:

 

Real-life example

Vending machine

Let’s now make an example of a machine. Suppose you want to do business by putting machines on the road that can sell food products and you need a program that will change the state of the machine, eg when we deposit cash then we change the state of the machine from pending state to cash on the product release state.

It is difficult to show the advantages of the State pattern in such simple examples, in such examples the State pattern adds only unnecessarily more code, it is used in bigger projects, but I will not make some long example then it will be hard to understand, but the following example should show how the State pattern is useful in large projects.

We will do in the order as in the previous example.

Let’s start with the Context class, in this case it will be called VendingMachine.

class VendingMachineState

And specific classes inheriting from the VendingMachineState class, which first is the VendingDepositeState class.

In this class, we have the conditions that if the customer pays too much, we pay the rest to the customer and approve the payment, if not enough, we display the message.

First class VendingStockState, which we give the customer the product for which he paid.

And finally the customer.

Result:

 

Relations with other design patterns

  • State pattern objects are often singletons.
  • The Flyweight pattern can use the State pattern to divide objects.
  • The interpreter can use the State pattern to define a Context class that parses all data.

The State pattern is also similar to the Strategy pattern that will be discussed in the next article 🙂

 

Summary

That’s all about State🙂.

Link to github with the whole code from this article:  https://github.com/Slaw145/State

This content also you can find on my steemit blog:  https://steemit.com/design-patterns/@slawas/design-patterns-state

And on medium:  https://medium.com/@sawomirkowalski/design-patterns-state-6e4ad27df0d7

In the next article, we will talk about the Strategy pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb: Devman.pl-Slawomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

Illustrations, pictures and diagrams are from: https://sourcemaking.com/design_patterns/state

 
If that post was useful for you share it with your friends :)

Post a comment

Be first!

avatar
  Subscribe  
Notify about