文章目录
  1. 1. 简单介绍
  2. 2. 适用性
  3. 3. UML图
    1. 3.1. 参与者
  4. 4. 实例
  5. 5. 输出
  6. 6. 效果

简单介绍

Mediator模式是行为模式之一,Mediator模式定义:用一个中介者对象来封装一系列的对象交互。中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立的改变他们之间的交互。

适用性

  1. 一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。
  2. 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。
  3. 想定制一个分布在多个类的行为,而又不想生成太多的子类。

UML图

参与者

  1. Mediator:Defines an interface for communicating with Colleague objects;Typically mediator is informed of some event or situation
  2. ConcreteMediator:Implements cooperative behavior by coordinating Colleague objects;Knows and maintains its colleagues
  3. Colleague Classes:Each Colleague class knows its Mediator object;Each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague;Offers services (requests) to mediator;Offers services (requests) to mediator

实例

  1. Mediator:

    public interface Mediator {
        public  void action(Colleague sender);
        public  void addColleague(Colleague colleague);
    }
    
  2. ConcreteMediator:

    public class ConcreteMediator implements Mediator{
        private List<Colleague> colleagues = new ArrayList<Colleague>();
    
        @Override
        public void action(Colleague sender) {
            String msg = sender.getMessage();
    
            for(Colleague colleague : colleagues) {
                if(colleague.equals(sender)) {
                    colleague.sendMsg();
                    break;
                }
            }
    
            for(Colleague colleague : colleagues) {
                if(colleague.equals(sender)) {
                    continue;
                } else {
                    colleague.getMsg(msg);
                }
            }
        }
    
        @Override
        public void addColleague(Colleague colleague) {
            colleagues.add(colleague);
        }
    
    }
    
  3. Colleague:

    public abstract class Colleague {
        private Mediator mediator;
        private String message;
    
        public Colleague(Mediator mediator) {
            this.mediator = mediator;
        }
    
        public Mediator getMediator() {
            return mediator;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public void sendMessage(String message) {
            this.message = message;
            mediator.action(this);
        }
    
        public abstract void sendMsg();
        public abstract void getMsg(String message);
    }
    
  4. Colleague1:

    public class Colleague1 extends Colleague{
    
        public Colleague1(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void sendMsg() {
             System.out.println("Colleague1 has sent the message -'" + getMessage() + "'");
        }
    
        @Override
        public void getMsg(String message) {
             System.out.println("Colleague1 has got the message -'" + message + "'");
    
        }
    }
    
  5. Colleague2:

    public class Colleague2 extends Colleague{
    
        public Colleague2(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void sendMsg() {
             System.out.println("Colleague2 has sent the message -'" + getMessage() + "'");
        }
    
        @Override
        public void getMsg(String message) {
             System.out.println("Colleague2 has got the message -'" + message + "'");
    
        }
    }
    
  6. Colleague3:

    public class Colleague3 extends Colleague{
    
        public Colleague3(Mediator mediator) {
            super(mediator);
        }
    
        @Override
        public void sendMsg() {
             System.out.println("Colleague3 has sent the message -'" + getMessage() + "'");
        }
    
        @Override
        public void getMsg(String message) {
             System.out.println("Colleague3 has got the message -'" + message + "'");
    
        }
    }
    
  7. Test:

    public class Test {
        public static void main(String arg[]) {
            Mediator mediator = new ConcreteMediator();
            Colleague colleague1 = new Colleague1(mediator);
            Colleague colleague2 = new Colleague2(mediator);
            Colleague colleague3 = new Colleague3(mediator);
    
            mediator.addColleague(colleague1);
            mediator.addColleague(colleague2);
            mediator.addColleague(colleague3);
    
            colleague1.sendMessage("hello,I'm Never!");
            System.out.println("================");
            colleague2.sendMessage("py is BT!");
            System.out.println("================");
            colleague3.sendMessage("ddddddd");
        }
    }
    

输出

Colleague1 has sent the message -'hello,I'm Never!'
Colleague2 has got the message -'hello,I'm Never!'
Colleague3 has got the message -'hello,I'm Never!'
================
Colleague2 has sent the message -'py is BT!'
Colleague1 has got the message -'py is BT!'
Colleague3 has got the message -'py is BT!'
================
Colleague3 has sent the message -'ddddddd'
Colleague1 has got the message -'ddddddd'
Colleague2 has got the message -'ddddddd'

效果

  1. 定义一个对象来封装一系列的对象交互(Define an object that encapsulates how a set of objects interact)
  2. 中介者通过明确指明对象之间的关系来改善解耦(Mediator promotes loose coupling by keeping objects from referring to each other explicitly)
  3. 独立改变他们的交互(Lets you vary their interaction independently)
  4. 集中控制(Lets you vary their interaction independently)
文章目录
  1. 1. 简单介绍
  2. 2. 适用性
  3. 3. UML图
    1. 3.1. 参与者
  4. 4. 实例
  5. 5. 输出
  6. 6. 效果