1. Overview
When there are multiple states for an object in the system, the state can be converted between them, and the object behaves differently in different states. The state mode separates the state of an object from the object and encapsulates it into a special state class, so that the object state can be flexibly changed. State mode is an object behavioral mode.
2. Applicable scenarios
Used to solve the encapsulation problem of multiple state transitions of complex objects in the system and behaviors under different states. Simply put, it is to deal with multiple states of objects and their mutual conversions.
III. UML class diagram
4. Participants
1), AbstractState (abstract state class):
The abstract state class defines the behavior abstract methods under different states, and different behavioral operations are implemented in subclasses (different state subclasses).
2) ConcreteState (state subclass that implements behavior in specific states):
Subclasses of abstract state classes, each subclass implements a behavior related to a state of the environment class (Context). Each specific state class corresponds to a specific state of the environment, and the behavior of different specific states is different.
3) Context (environment class that owns state objects):
Having state attributes, due to the diversity of the environment, it can have different states and behave differently under different states. Maintain an abstract state instance in the environment class. This instance defines the state of the current environment (setState() method), and separates the specific state behavior and is completed by different state subclasses.
5. Use case learning
1. Abstract state class: State.java
/** * State mode of JAVA design pattern* Abstract state class* @author [email protected] * */ public abstract class State { /** * State behavior abstract method, the specific state subclass implements different behavior logic*/ public abstract void Behavior(); }
2. Specific state subclass A: ConcreteStateA.java
/** * Specific status subclass A * @author [email protected] */ public class ConcreteStateA extends State { @Override public void Behavior() { // Business behavior of state A, and what can it do when it is in this state // For example: the mobile phone can make calls normally when it is not down due to down payment System.out.println("The mobile phone can make calls normally when it is not down payment"); } }
3. Specific state subclass B: ConcreteStateB.java
/** * Specific state subclass B * @author [email protected] * */ public class ConcreteStateB extends State { @Override public void Behavior() { // Business behavior of state B, and what can it do when it is in this state // For example: when the mobile phone is in arrears and downtime, System.out.println("The mobile phone is in arrears and downtime, cannot make calls"); } }
4. Environment class with state objects: Context.java
/** * Environment/Context class<br/> * Has a state object, and can complete the transition between states [state changes/switching is implemented in the environment class] * @author [email protected] * */ public class Context { // Maintain a reference to an abstract state object private State state; /* * Simulate mobile phone phone call fee attributes<br/> * The environment state is as follows: * 1>, when bill >= 0.00$: When the state is normal, you can still make calls* 2>, when bill < 0.00$: When the phone is owed, you cannot make calls*/ private double bill; /** * Environment processing function, calling state instance behavior completes business logic<br/> * References to different behaviors in different states*/ public void Handle(){ checkState(); state.Behavior(); } /** * Check the environment state: state changes/switching is implemented in the environment class*/ private void checkState(){ if(bill >= 0.00){ setState(new ConcreteStateA()); } else { setState(new ConcreteStateB()); } } /** * Set the environment state<br/> * Private method, the purpose is to let the state of the environment be controlled/switched by the system environment itself, and external users do not need to care about the state inside the environment* @param state */ private void setState(State state){ this.state = state; } public double getBill() { return bill; } public void setBill(double bill) { this.bill = bill; } }
5. Test client calling class: Client.java
public class Client { public static void main(String[] args) { Context context = new Context(); context.setBill(5.50); System.out.println("Current call fee balance: " + context.getBill() + "$"); context.Handle(); context.setBill(-1.50); System.out.println("Current call fee balance: " + context.getBill() + "$"); context.Handle(); context.setBill(50.00); System.out.println("Current call fee balance:" + context.getBill() + "$"); context.Handle(); } } 6. Program operation results:
Current call fee balance: 5.5$
The current phone bill balance can be made normally when the phone is not down due to down payment: -1.5$
The current phone fee balance is not allowed to make calls when the phone is out of service.
The phone can make calls normally without any down payment
VI. Expand
There are two different implementations of state switching in state mode
Method 1: Change/switching of state is implemented in the environment class. As in the use case code above, the checkState() method in the Context class.
/** * Check the environment state: state changes/switching is implemented in the environment class*/ private void checkState(){ if(bill >= 0.00){ setState(new ConcreteStateA()); } else { setState(new ConcreteStateB()); } } Method 2: Change/switching of state is implemented in a specific state subclass.
The implementation steps are as follows:
1) Initialize a state instance object in the environment class Context class, and pass the environment Context object as a constructor parameter of the subclass state to the specific state subclass instance.
For example, in the Context.java class
// Set the initial state this.state = new ConcreteStateA(this);
2) In the specific subclass state class, according to the constructed context object, the business logic judges the status by calling the property value of the context object.
For example, in the ConcreteStateA.java class, the specific state subclass:
/** * Specific state subclass A * @author [email protected] */ public class ConcreteStateA extends State { private Context ctx; public ConcreteStateA(Context context){ ctx = context; } @Override public void Behavior() { // Business behavior of state A, and what can it do when it is in this state // For example: the mobile phone can make calls normally when it is not down due to down payment System.out.println("The mobile phone can make calls normally when it is not down payment"); checkState(); } /** * Check whether the state needs to be converted<br/> * The switching of state is implemented in the specific state subclass */ private void checkState(){ if (ctx.getBill() < 0.00) { ctx.setState(new ConcreteStateB(ctx)); } } }
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.