关于Java 设计模式 中介者模式 Mediator

关于Java 设计模式 中介者模式 Mediator

1、Order.java

package com.ssm.cts.pattern.mediator;

public class Order {

  public void buyBar(int number) {
    Stock stock = new Stock();
    stock.increase(number);
    System.out.println("采购人员采购汽车:" + number + "台");
  }

}

2、Sale.java

package com.ssm.cts.pattern.mediator;

public class Sale {

  public void saleCar(int number) {
    // 访问库存
    Stock stock = new Stock();
    
    stock.decrease(number);
    System.out.println("销售人员销售汽车:" + number + "台");
  }
}

3、Stock.java

package com.ssm.cts.pattern.mediator;

public class Stock {
  // 初始库存
  private static int CAR_NUMBER = 100;

  // 库存增加
  public void increase(int number) {
    CAR_NUMBER = CAR_NUMBER + number;
  }

  // 库存降低
  public void decrease(int number) {
    CAR_NUMBER = CAR_NUMBER - number;
  }

  // 库存数量
  public int getStockNumber() {
    return CAR_NUMBER;
  }
  
  // 库存盘点
  public void check() {
    System.out.println("库存数量为:" + CAR_NUMBER);
  }
}

4、Demo.java

package com.ssm.cts.pattern.mediator;

public class Demo {

  // 测试
  public static void main(String[] args) {
    // 采购
    System.out.println("采购");
    Order purchase = new Order();
    purchase.buyBar(5);

    // 销售
    System.out.println("销售");
    Sale sale = new Sale();
    sale.saleCar(1);

    // 盘点
    System.out.println("库存盘点");
    Stock stock = new Stock();
    stock.check();
  }

}

 

发表回复

您的电子邮箱地址不会被公开。