Category: Behavioral design patterns

Purpose: Objects exhibit a different set of behaviors in different states, and running states can be switched

The complete code for reference: ct0-my.sharepoint.com/:u:/g/perso…

A typical scenario

Role: nurse, patient Scenario: Patients will make different behaviors for the nurse’s care according to their spontaneous state

The patient is in stable condition

  1. The nurse takes the patient for a walk and the patient will be in stable condition
  2. Give oxygen to a stable patient and the patient will go from stable to unstable

The patient is in an unstable state

  1. The nurse takes the patient for a walk. The patient will remain in an unstable condition and may be at home
  2. Oxygen is given to a stable patient, and the patient changes from an unstable state to a stable state

Pattern implementation

The Nurse class Nurse. Java

public class Nurse {

    PatientStateBehavior state;
    StablePatientStateBehavior stablePatientState = new StablePatientStateBehavior(this);
    UnStablePatientStateBehavior unStablePatientState = new UnStablePatientStateBehavior(this);

    public void setState (PatientStateBehavior state) {
        this.state = state;
    }

    public void supplyOxygen (a) {
        state.supplyOxygen();
    }

    public void walking(a) { state.walking(); }}Copy the code

Steady state behavior class in patients behavior StablePatientStateBehavior. Java, patients with unstable behavior UnStablePatientStateBehavior. Java

public interface PatientStateBehavior {

    void supplyOxygen (a);

    void walking(a);

}

class StablePatientStateBehavior implements PatientStateBehavior {

    private Nurse context;

    public StablePatientStateBehavior (Nurse context) {
        this.context = context;
    }

    @Override
    public void supplyOxygen (a) {
        System.out.println("Giving oxygen to a stable patient may cause the patient to become unstable.");
        context.setState(context.unStablePatientState);
    }

    @Override
    public void walking(a) {
        System.out.println("Walking stable patients improves resistance."); context.setState(context.stablePatientState); }}class UnStablePatientStateBehavior implements PatientStateBehavior {

    private Nurse context;

    public UnStablePatientStateBehavior (Nurse context) {
        this.context = context;
    }

    @Override
    public void supplyOxygen (a) {
        System.out.println("Giving oxygen to an unstable patient can make the patient stable.");
        context.setState(context.stablePatientState);
    }

    @Override
    public void walking(a) {
        System.out.println("Unstable patients don't need to walk."); context.setState(context.unStablePatientState); }}Copy the code

It can be seen that when patients are given different operations, the patient’s stable and unstable states switch according to whether the behavior is appropriate

The execution effect is as follows

var nurse = new Nurse();
System.out.println("Patients with initial instability :");
nurse.setState(nurse.unStablePatientState);
nurse.supplyOxygen(); // The patient has a state switch, unstable -> stable
nurse.walking();

System.out.println("\n Initially stable patient :");
nurse.setState(nurse.stablePatientState);
nurse.supplyOxygen(); // The patient underwent state transition, stable -> unstable
nurse.walking();
Copy the code

UML

A couple of points to note

If you can’t extend the new state, you can use a simple if else structure

The actual running implementations of objects implementing the same interface can change to behave differently

When the policy mode is executed, the self-generated policy object does not change. When the state mode is executed, the self-generated state object changes

The state mode can deal with the behavior of all states, while the policy mode generally deals with different policies of one behavior

The resources

  1. www.geeksforgeeks.org/state-desig…
  2. Stackoverflow.com/questions/1…
  3. Javarevisited.blogspot.com/2014/04/dif…