When the condition expression of the control object state is too complex, the judgment logic of the state is transferred to a series of classes or methods that represent different states, which can simplify the complicated logic judgment.

The state pattern allows an object to change its behavior when its internal state changes, and the object appears to modify its class.

This type of design pattern is behavioral.

The key of state mode is to distinguish the internal state of things. The change of internal state of things often brings about the change of behavior of things.

In state mode, we create objects that represent various states and a context object whose behavior changes as the state object changes.

introduce

Intent: Allows an object to change its behavior when its internal state changes, as if the object has modified its class.

The behavior of an object depends on its state (properties) and can change its behavior as its state changes.

When to use: The code contains a large number of conditional statements related to object state.

How to solve it: Abstract out the various concrete state classes.

Usage Scenarios:

  1. A situation in which behavior changes as the state changes.
  2. A substitute for a condition or branch statement.

Note: Use state mode when behavior is constrained by state, and there are no more than five states.

The advantages and disadvantages

Advantages:

  1. Encapsulates the transformation rules.
  2. Enumerates possible states. The type of state needs to be determined before enumerating states.
  3. Put all the behavior associated with a state into a single class, and you can easily add new states by simply changing the object’s state to change its behavior.
  4. Allows state transition logic to be integrated with state objects, rather than some giant block of conditional statements.
  5. You can reduce the number of objects in the system by having multiple environment objects share a state object.

Disadvantages:

  1. The use of state patterns inevitably increases the number of system classes and objects.
  2. The structure and implementation of state pattern are complicated, and it will lead to confusion of program structure and code if used improperly.
  3. State modes do not support the “on/off principle” very well. For state modes that can be switched, adding a new state class requires modifying the source code responsible for the transition to the new state, and modifying the behavior of a state class requires modifying the source code of the corresponding class.

application

  • File download (start, pause, finish, fail, etc.)
  • Games (walking, attacking, defending, falling, jumping)
  • Traffic light (red, green, yellow switch)

Relationship between state patterns and policy patterns

The state pattern and the policy pattern are like twins, both encapsulating a set of algorithms or behaviors, but very different in intent, so they are very different patterns.

Policy patterns and state patterns are similar in that they both have a context, policies, or state classes to which the context delegates requests.

The difference between them is that each policy class in the strategy mode is equal and parallel, without any connection between them. Therefore, customers must be familiar with the role of these policy classes, so that customers can actively switch algorithms at any time. However, in the state mode, the state and the behavior corresponding to the state have long been encapsulated, and the switch between the states has long been specified, and the “behavior change” happens within the state mode. These details are not necessary for the customer. This is where state patterns come in.

conclusion

State patterns can be used to optimize scenarios where conditional branching statements have a large number of states and the behavior changes with state changes. It’s not easy to understand at first, but when used properly it makes the structure of your code very clear and extensible.

reference

  • JavaScript design patterns and development practices
  • Design Mode – Novice tutorial