23 Design Patterns (22) - Status Patterns

Keywords: Mobile Java Attribute

1. Overview

When there are multiple states for an object in the system, these states can be transformed, and the state mode can be used when 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 state of the object can be flexibly changed.State mode is an object behavior mode.

 

2. Scenarios applicable

Solves multiple state transitions of complex objects in the system and encapsulation of behavior in different states.Simply put, it deals with the various states of objects and their transitions to each other.

 

3. UML Class Diagram

 

IV. Participants

1>, AbstractState (abstract state class):

Define behavior abstraction methods in abstract state classes that state behavior in different states, and implement different behavior actions in subclasses (different state subclasses).

2>, ConcreteState (state subclass that implements behavior in a specific state):

Subclasses of abstract state classes, each of which implements a behavior related to a state of the environment class (Context), each of which corresponds to a specific state of the environment and behaves differently depending on the specific state.

3>, Context (environment class with state object):

Has a status attribute, which can have different states and behave differently depending on the diversity of the environment.Maintains an abstract state instance in an environment class that defines the state of the current environment (setState() method) and separates specific state behavior from different state subclasses.

 

V. Use Case Learning

1. Abstract state class: State.java

/** 
* JAVA State Mode of Design Mode
* Abstract State Class
* @author  lvzb.software@qq.com 
* 
*/  
public abstract class State {  
   /** 
    * State behavior abstraction method, which implements different behavior logic by specific state subclasses
    */  
   public abstract void Behavior();  
 
}

 

2. Specific state subclass A:ConcreteStateA.java

/** 
* Specific state subclass A
* @author  lvzb.software@qq.com 
*/  
public class ConcreteStateA extends State {  
 
   @Override  
   public void Behavior() {  
       // Business behavior of state A and what to do when in that state.
       // For example: the mobile phone can make phone calls normally when it is down without payment.
       System.out.println("Mobile phone is down without payment, Call normally");  
   }  
 
}

 

3. Specific Status Subclass B: ConcreteStateB.java

/** 
* Specific State Subclass B
* @author  lvzb.software@qq.com 
* 
*/  
public class ConcreteStateB extends State {  
 
   @Override  
   public void Behavior() {  
       // Business behavior of state B and what to do when in that state
       // For example: the mobile phone cannot make a call when it is in arrears and down.
       System.out.println("Mobile phone in arrears downtime, Cannot make a call");  
   }  
 
}

 

4. Environment class with state object: Context.java

/** 
* Environment/Context Class <br/>
* Has state objects and can complete transitions between states [state changes/transitions implemented in environment classes]
* @author  lvzb.software@qq.com 
* 
*/  
public class Context {  
   // Maintain a reference to an abstract state object
   private State state;  
     
   /* 
    * Simulated Phone Call Attribute <br/>
    * The environment status is as follows:
    * 1>,When bill >= 0.00$: is in normal condition and can make phone calls
    * 2>,When bill < 0.00$: the phone is in arrears and cannot make a call
    */  
   private double bill;  
     
   /** 
    * Environment handler, call state instance behavior to complete business logic <br/>.
    * Processing different behaviors in different states based on different state instance references
    */  
   public void Handle(){  
       checkState();  
       state.Behavior();  
   }  
     
     
   /** 
    * Check environment state: state change/switch is implemented in environment class
    */  
   private void checkState(){  
       if(bill >= 0.00){  
           setState(new ConcreteStateA());  
       } else {  
           setState(new ConcreteStateB());  
       }  
   }  
     
     
   /** 
    * Set environment state <br/>.
    * Private method to allow the state of the environment to be controlled/switched by the system environment itself, without external users having 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 Call 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 balance:" + context.getBill() + "$");  
       context.Handle();  
         
       context.setBill(-1.50);  
       System.out.println("Current call balance:" + context.getBill() + "$");  
       context.Handle();  
         
       context.setBill(50.00);  
       System.out.println("Current call balance:" + context.getBill() + "$");  
       context.Handle();  
   }  
}

 

6. Results of program operation:

Current call balance: $5.5
 The mobile phone can make phone calls normally when it is down without payment.
Current call balance: -1.5$.
The mobile phone cannot make a call when it is in arrears and down.
Current call balance: $50.0
 The mobile phone can make phone calls normally when it is down without payment

 

6. Extension

There are two different implementations of state switching in state mode

Mode 1: State change/switch is implemented in the environment class.Such as the checkState() method in the Context class of the use case code above.

/** 
    * Check environment state: state change/switch is implemented in environment class
    */  
   private void checkState(){  
       if(bill >= 0.00){  
           setState(new ConcreteStateA());  
       } else {  
           setState(new ConcreteStateB());  
       }  
   }

 

Mode 2: State change/switch is implemented in a specific state subclass.

The steps to achieve this are as follows:

1>Initialize a state instance object in the Context class of the environment class and pass the environment Context object as a construction parameter for the subclass state to the specific state subclass instance.

As in the Context.java class

// Set initial state
this.state = new ConcreteStateA(this);

 

2>In the specific subclass status class, according to the constructed context object, check and switch the status by calling the attribute value of the context object to make business logic judgment.

 

As in the ConcreteStateA.java class of the specific state subclass:

/** 
* Specific state subclass A
* @author  lvzb.software@qq.com 
*/  
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 to do when in that state.
       // For example: the mobile phone can make phone calls normally when it is down without payment.
       System.out.println("Mobile phone is down without payment, Call normally");  
       checkState();  
         
   }  
 
   /** 
    * Check if state transition is required <br/>.
    * Switching States is accomplished in a specific state subclass
    */  
   private void checkState(){  
       if (ctx.getBill() < 0.00) {  
           ctx.setState(new ConcreteStateB(ctx));  
       }  
   }  
}
Forty-five original articles have been published. 1. Visits 1039
Private letter follow

Posted by john_6767 on Mon, 09 Mar 2020 20:21:34 -0700