中介者模式

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicity, and it lets you vary interaction independently.

中介者模式的通用类图

中介者模式的通用源码

  • 抽象中介者类

    public abstract class AbstractMediator {
    
        protected ConcreteColleague1 c1;
        protected ConcreteColleague2 c2;
    
        //----通过getter/setter把同事的类注入进来
        public ConcreteColleague1 getC1() {
            return c1;
        }
        public void setC1(ConcreteColleague1 c1) {
            this.c1 = c1;
        }
        public ConcreteColleague2 getC2() {
            return c2;
        }
        public void setC2(ConcreteColleague2 c2) {
            this.c2 = c2;
        }
    
        //--中介者模式的业务逻辑
        public abstract void doSomething1();
        public abstract void doSomething2();
    }
    
  • 具体中介者类

    public class ConcreteMediator extends AbstractMediator {
    
        @Override
        public void doSomething1() {
    
        }
    
        @Override
        public void doSomething2() {
    
        }
    }
    
  • 抽象同事类

    public abstract class AbstractColleage {
    
        protected ConcreteMediator mediator = null;
        public AbstractColleage(ConcreteMediator _mediator) {
            this.mediator = _mediator;
        }
    }
    
  • 具体同事类1

    public class ConcreteColleague1 extends AbstractColleage {
    
        //通过构造函数传入中介者
        public ConcreteColleague1(ConcreteMediator _mediator) {
            super(_mediator);
        }
    
        //自有的方法
        public void selfMethod1() {
            //处理自己的业务逻辑
        }
    
        //依赖的方法
        public void depMethod1() {
            //处理自己的业务逻辑
            //自己不能处理的业务逻辑,委托给中介者处理
            super.mediator.doSomething1();
        }
    }
    
  • 具体同事类2

    public class ConcreteColleague2 extends AbstractColleage {
    
        //通过构造函数传入中介者
        public ConcreteColleague2(ConcreteMediator _mediator) {
            super(_mediator);
        }
    
        //自有的方法
        public void selfMethod2() {
            //处理自己的业务逻辑
        }
    
        //依赖的方法
        public void depMethod2() {
            //处理自己的业务逻辑
            //自己不能处理的业务逻辑,委托给中介者处理
            super.mediator.doSomething2();
        }
    }
    

中介者模式的优点

  • 减少了类间的依赖,把原来的一对多的依赖变成了一对一的依赖,同事类只依赖中介者,减少了依赖,当然同时也降低类间的耦合。

中介者模式的缺点

  • 中介者会膨胀很大,而且逻辑复杂,原本N个对象直接的相互依赖关系转换为中介者和同事类的依赖关系,同事类越多,中介者的逻辑月复杂。

中介者模式的使用场景

如:
- 机场调度中心
- MVC框架
- 媒体网关(MSN)
- 中介服务

注意:
这里说明的时候同事类,而不是兄弟类。

中介者模式的简单应用类图及源码

中介者模式_第1张图片

  • 抽象中介者类

    public abstract class AbstractMediator {
    
        protected Purchase purchase;
        protected Stock stock;
        protected Sale sale;
    
        public AbstractMediator() {
            purchase = new Purchase(this);
            stock = new Stock(this);
            sale = new Sale(this);
        }
    
        //中介者最重要的方法->时间处理,处理抖个对象之间的关系
        public abstract void execute(String string, Object...objects);
    }
    
  • 具体中介者类

    public class Mediator extends AbstractMediator {
    
        //中介者最重要的方法
        @Override
        public void execute(String string, Object... objects) {
            if(string.equals("purchase.buy")) { //采购电脑
                this.buyComputer((Integer)objects[0]);
            }
            else if(string.equals("sale.sell")) { //销售电脑
                this.sellComputer((Integer)objects[0]);
            }
            else if(string.equals("sale.offsell")) { //折价销售
                this.offSell();
            }
            else if (string.equals("stock.clear")) { //清仓处理
                this.clearStock();
            }
        }
    
        //采购电脑
        private void buyComputer(int number) {
            int saleStatus = super.sale.getSaleStatus();
            System.out.println(saleStatus);
            if (saleStatus > 80) { //销售情况好
                System.out.println("采购IBM电脑:" + number + "台");
                super.stock.increase(number);
            }
            else { //销售情况不好
                int buyNumber = number / 2;
                System.out.println("采购IBM电脑:" + buyNumber + "台");
                //super.stock.increase(buyNumber);
            }
        }
    
        //销售电脑
        private void sellComputer(int number) {
            if (super.stock.getStockNumber() < number) {
                super.purchase.buyIBMComputer(number);
            }
            super.stock.decrease(number);
        }
    
        //折价销售电脑
        private void offSell() {
            System.out.println("折价销售IBM电脑" + stock.getStockNumber() + "台");
        }
    
        //清仓处理
        private void clearStock() {
            super.sale.offSale();
            super.purchase.refuseBuyIBM();
        }
    }
    
  • 抽象同事类

    public abstract class AbstractColleague {
    
        protected AbstractMediator mediator;
    
        public AbstractColleague(AbstractMediator _mediator) {
            this.mediator = _mediator;
        }
    }
    
  • 采购类(具体同事类1)

    public class Purchase extends AbstractColleague {
    
        public Purchase(AbstractMediator _mediator) {
            super(_mediator);
        }
    
        //采购IBM电脑
        public void buyIBMComputer(int number) {
            super.mediator.execute("purchase.buy", number);
        }
    
        //不再采购IBM电脑
        public void refuseBuyIBM() {
            System.out.println("不再采购IBM电脑");
        }
    }
    
  • 销售类(具体同事类2)

    public class Sale extends AbstractColleague {
    
        public Sale(AbstractMediator _mediator) {
            super(_mediator);
        }
    
        //销售IBM电脑
        public void sellIBMComputer(int number) {
            super.mediator.execute("sale.sell", number);
            System.out.println("销售IBM电脑" + number + "台");
        }
    
        //反馈销售情况
        public int getSaleStatus() {
            Random random = new Random();
            int saleStatus = random.nextInt(100);
            System.out.println("IBM电脑的销售情况为:" + saleStatus);
            return saleStatus;
        }
    
        //折价处理
        public void offSale() {
            super.mediator.execute("sale.offsell");
        }
    }
    
  • 库存类(具体同事类3)

    public class Stock extends AbstractColleague {
    
        public Stock(AbstractMediator _mediator) {
            super(_mediator);
        }
    
        //开始有100台电脑
        private static int COMPUTER_NUMBER = 100;
    
        //库存增加
        public void increase(int number) {
            COMPUTER_NUMBER = COMPUTER_NUMBER + number;
            System.out.println("库存数量为:" + COMPUTER_NUMBER);
        }
    
        //库存减少
        public void decrease(int number) {
            COMPUTER_NUMBER = COMPUTER_NUMBER - number;
            System.out.println("库存数量为:" + COMPUTER_NUMBER);
        }
    
        //获得数量
        public int getStockNumber() {
            return COMPUTER_NUMBER;
        }
    
        public void clearStock() {
            System.out.println("清理存货数量为:" + COMPUTER_NUMBER);
            super.mediator.execute("stock.clear");
        }
    }
    
  • 场景类

    public class Client {
    
        public static void main(String[] args) {
            AbstractMediator mediator = new Mediator();
            System.out.println("------采购-------");
            Purchase purchase = new Purchase(mediator);
            purchase.buyIBMComputer(100);
            System.out.println("\n------销售-------");
            Sale sale = new Sale(mediator);
            sale.sellIBMComputer(1);
            System.out.println("\n------库存-------");
            Stock stock = new Stock(mediator);
            stock.clearStock();
        }
    }
    

你可能感兴趣的:(中介者模式)