I. Model examples
1.1, principle
1, definition
-
In State Pattern, the behavior of a class is based on its state changes. This type of design pattern belongs to behavioral pattern.
-
In the state mode, we create an object representing various states and a context object whose behavior changes as the state object changes.
2, introduction
-
Intention: Allows an object to change its behavior when its internal state changes, and the object appears to modify its class.
-
The main solution: the object's behavior depends on its state (property), and can change its related behavior according to its state change.
-
When to use: The code contains a large number of conditional statements related to object state.
-
How to solve this problem: abstract all kinds of concrete state classes.
-
Key code: Usually there is only one method in the command mode interface. There is one or more methods in the state mode interface. Moreover, the method of implementing the class of the state pattern usually returns the value or changes the value of the instance variable. That is to say, the state pattern is generally related to the state of the object. The methods that implement classes have different functions, covering the methods in interfaces. Like command mode, state mode can also be used to eliminate if. Conditional selection statements such as else.
-
Application examples:
1. Players can play basketball in normal, abnormal and abnormal conditions.
2. In the bell chimes of Zeng Hou Yi,'bell is abstract interface','bell A'and so on are concrete states,'bell chimes of Zeng Hou Yi' are concrete environments. -
Advantage:
1. The transformation rules are encapsulated.
2. Enumeration of possible states. Before enumerating states, we need to determine the types of states.
3. Put all the behavior related to a state into a class, and add new states conveniently. Only changing the state of the object can change the behavior of the object.
4. Allow state transition logic to be integrated with state objects rather than a huge conditional block.
5. It can let multiple environment objects share one state object, thus reducing the number of objects in the system. -
Disadvantages:
1. The use of state mode will inevitably increase the number of system classes and objects.
2. The structure and implementation of state mode are very complex. If used improperly, it will lead to confusion of program structure and code.
3. The state mode does not support the "Open-Close Principle" very well. For the state mode that can switch state, adding new state classes needs to modify the source code responsible for state transition. Otherwise, it can not switch to the new state, and modifying the behavior of a state class also needs to modify the source code of the corresponding class. -
Use scenarios:
1. Scenarios in which behavior changes with state.
2. The substitutes of condition and clause. -
Note: Use state mode when behavior is constrained by state, and the state should not exceed 5.
3, UML diagram
4, demand
- The product is a food meal. According to the time of the food, the time of the meal is determined by the time of the food: breakfast, lunch, dinner, or undefined.
1.2, code
1. Product Category: food Dining
public class Food { private int hour; private State state; public int getHour() { return hour; } public void setHour(int hour) { this.hour = hour; } public Food() { state = new BreakfastState(); } public void doSomething(){ state.doSomething(this); state = new BreakfastState(); } }
2. Processing state abstract classes
public abstract class State { public abstract void doSomething(Food food); }
3. Specific Status Category: Breakfast
public class BreakfastState extends State{ @Override public void doSomething(Food food) { if (food.getHour() == 7){ System.out.println("It's breakfast time now."); }else{ LunchState lunchState = new LunchState(); lunchState.doSomething(food); } } }
4. Specific Status Category: Lunch
public class LunchState extends State { @Override public void doSomething(Food food) { if(food.getHour() == 12){ System.out.println("It's lunchtime now."); }else{ DinnerState dinnerState = new DinnerState(); dinnerState.doSomething(food); } } }
5. Specific State Processing Category: Dinner
public class DinnerState extends State { @Override public void doSomething(Food food) { if (food.getHour() == 18){ System.out.println("It's dinner time now."); }else{ NoState noState = new NoState(); noState.doSomething(food); } } }
6. Specific state class: time undefined
public class NoState extends State { @Override public void doSomething(Food food) { System.out.println(food.getHour() + "This time state is undefined"); } }
7. Startup class
public class StateApplication { public static void main(String[] args) { Food food = new Food(); food.setHour(7); food.doSomething(); food.setHour(12); food.doSomething(); food.setHour(18); food.doSomething(); food.setHour(8); food.doSomething(); food.setHour(13); food.doSomething(); food.setHour(7); food.doSomething(); } }