23 Design Patterns - State Pattern

Introduction

You can compare and refer to learning----state mode example

The state pattern is a type of behavioral design pattern. Its design philosophy is that when the internal state of the object changes, its behavior will be changed accordingly. There is a one-to-one correspondence between state and behavior.

This pattern is mainly used when the behavior of an object depends on its state, and its behavior is switched as the state changes.

State Pattern UML Class Diagram

insert image description here
Class Diagram Explanation

  • State: Abstract state interface (can also be defined as an abstract class), which encapsulates the behaviors corresponding to all states.
  • ConcreteStateA/B: Concrete state class, which implements the abstract state interface, implements the methods defined in the interface according to its corresponding state, and has another function to indicate how to transition to the next state.
  • Context: Environment (context) role, this class is responsible for state switching, and also holds a State instance, representing the state of the current environment.

Case explanation

Case: Realize the function of self-service vending machine through state mode.

state interface

//状态接口
public interface State {
    
    
    // 挑选商品
    void choose();
    // 付款
    boolean payment();
    // 分发商品
    void dispenseCommodity();
}

Select product status category


//挑选商品状态类
public class ChooseGoods implements State {
    
    

    VendingMachine machine;

    public ChooseGoods(VendingMachine machine) {
    
    
        this.machine = machine;
    }

    @Override
    public void choose() {
    
    
        if (machine.getCount() > 0) {
    
    
            System.out.println("商品挑选成功,请及时付款!");
            machine.setState(machine.getPaymentState());
        } else {
    
    
            System.out.println("很遗憾,商品售罄了!");
            machine.setState(machine.getEmptyState());
        }
    }

    @Override
    public boolean payment() {
    
    
        System.out.println("请先挑选商品!");
        return false;
    }

    @Override
    public void dispenseCommodity() {
    
    
        System.out.println("请先挑选商品!");
    }
}

Payment status class


import java.util.Random;

//付款状态类
public class PaymentState implements State {
    
    

    VendingMachine machine;

    public PaymentState(VendingMachine machine) {
    
    
        this.machine = machine;
    }

    @Override
    public void choose() {
    
    
        System.out.println("商品已选购完成请勿重复挑选");
    }

    @Override
    public boolean payment() {
    
    
        Random random = new Random();
        int num = random.nextInt(10);
        if(num % 2 == 0){
    
    
            System.out.println("付款成功!");
            machine.setState(machine.getDispenseCommodityState());
            return true;
        }
        System.out.println("付款失败,请重新支付!");
        return false;
    }

    @Override
    public void dispenseCommodity() {
    
    
        System.out.println("请先完成支付!");
    }
}

Product sold out status class


//商品售罄状态类
public class EmptyState implements State {
    
    

    VendingMachine machine;

    public EmptyState(VendingMachine machine) {
    
    
        this.machine = machine;
    }

    @Override
    public void choose() {
    
    
        System.out.println("对不起商品已售罄!");
    }

    @Override
    public boolean payment() {
    
    
        System.out.println("对不起商品已售罄!");
        return false;
    }

    @Override
    public void dispenseCommodity() {
    
    
        System.out.println("对不起商品已售罄!");
    }
}

Distribute product status class


//分发商品状态类
public class DispenseCommodityState implements State {
    
    

    VendingMachine machine;

    public DispenseCommodityState(VendingMachine machine) {
    
    
        this.machine = machine;
    }

    @Override
    public void choose() {
    
    
        System.out.println("请及时取走您的商品!");
    }

    @Override
    public boolean payment() {
    
    
        System.out.println("请及时取走您的商品!");
        return false;
    }

    @Override
    public void dispenseCommodity() {
    
    
        System.out.println("请及时取走您的商品!");
        machine.setState(machine.getChooseGoods());
    }
}

Vending machine => Context role

public class VendingMachine {
    
    
  // 表示当前状态
  private State state = null;
  // 商品数量
  private int count = 0;
  private State chooseGoods = new ChooseGoods(this);
  private State paymentState = new PaymentState(this);
  private State dispenseCommodityState = new DispenseCommodityState(this);
  private State emptyState = new EmptyState(this);

  public VendingMachine(int count) {
    
    
    this.count = count;
    this.state = this.getChooseGoods();
  }

  // 购买商品
  public void purchase() {
    
    
    // 挑选商品
    state.choose();
    // 支付成功
    if (state.payment()) {
    
    
      // 分发商品
      state.dispenseCommodity();
    }
  }
  
  // 获取商品后将商品减一
  public int getCount() {
    
    
    return count--;
  }
  
  // get和set方法 ... 
}

client test class

//客户端测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        VendingMachine machine = new VendingMachine(34);
        for (int i = 1; i < 30; i++) {
    
    
            System.out.println("第" + i + "次购买。");
            machine.purchase();
        }
    }
}

Guess you like

Origin blog.csdn.net/Ghoul___/article/details/126126322