中介者模式-大话设计模式

简介: 中介者模式-大话设计模式

一、介绍

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

Colleague叫作抽象同事类,而ConcreteColleague是具体同事类,每个具体同事只知道自己的行为,而不了解其他同事类的情况,但他们却都认识中介者对象,Mediator是抽象中介者,定义了同事对象到中介者对象的接口,ConcreteMediator是具体中介者对象,实现抽象类的方法,它需要知道所有具体同事类,并从具体同事接收消息,向具体同事对象发出命令。  

二、代码实现

//抽象同事类
public abstract class Colleague {
    //中介对象
    protected Mediator mediator;
 
    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }
}
 
 
public class ConcreteColleague1 extends Colleague{
    public ConcreteColleague1(Mediator mediator) {
        super(mediator);
    }
 
    public void send(String message){
        this.mediator.send(message,this);
    }
 
    public void notify(String message){
        System.out.println("同事1得到消息:"+message);
    }
 
}
 
 
public class ConcreteColleague2 extends Colleague{
    public ConcreteColleague2(Mediator mediator) {
        super(mediator);
    }
 
    public void send(String message){
        this.mediator.send(message,this);
    }
 
    public void notify(String message){
        System.out.println("同事2得到消息:"+message);
    }
 
}
 
//中介类
public abstract class Mediator {
    //抽象消息发送类
    public abstract void send(String message, Colleague colleague);
}
 
public class ConcreteMediator extends Mediator {
    private ConcreteColleague1 colleague1;
    private ConcreteColleague2 colleague2;
 
    public void setColleague1(ConcreteColleague1 colleague1) {
        this.colleague1 = colleague1;
    }
 
    public void setColleague2(ConcreteColleague2 colleague2) {
        this.colleague2 = colleague2;
    }
 
    @Override
    public void send(String message, Colleague colleague) {
        if (colleague == colleague1) {
            colleague2.notify(message);
        } else {
            colleague1.notify(message);
        }
    }
}

测试

public class Client {
    public static void main(String[] args) {
    ConcreteMediator m=new ConcreteMediator();
 
        ConcreteColleague1 c1=new ConcreteColleague1(m);
        ConcreteColleague2 c2=new ConcreteColleague2(m);
 
        m.setColleague2(c2);
        m.setColleague1(c1);
 
        c1.send("hello");
        c2.send("hello two");
 
    }
}


目录
相关文章
|
8月前
|
设计模式 前端开发
设计模式21 - 中介者模式【【Mediator Pattern】
设计模式21 - 中介者模式【【Mediator Pattern】
30 0
|
8月前
|
设计模式 调度
行为型设计模式09-中介者模式
行为型设计模式09-中介者模式
25 0
|
8月前
|
设计模式 C++
设计模式之中介者模式(C++)
设计模式之中介者模式(C++)
|
4天前
|
设计模式 Java 程序员
Java设计模式之中介者模式详解
Java设计模式之中介者模式详解
|
10天前
|
设计模式
设计模式之中介者模式
设计模式之中介者模式
|
23天前
|
设计模式 前端开发 NoSQL
设计模式第八讲:观察者模式和中介者模式详解
 定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。中介者模式又叫调停模式,它是迪米特法则的典型应用。
177 0
|
1月前
|
设计模式 Go
[设计模式 Go实现] 行为型~中介者模式
[设计模式 Go实现] 行为型~中介者模式
|
1月前
|
设计模式 Java
小谈设计模式(26)—中介者模式
小谈设计模式(26)—中介者模式
|
1月前
|
设计模式 Java
23种设计模式,中介者模式的概念优缺点以及JAVA代码举例
【4月更文挑战第8天】中介者模式是一种行为设计模式,它通过一个中介对象来封装一系列对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
48 8
|
1月前
|
设计模式 调度
【设计模式系列笔记】中介者模式
中介者模式(Mediator Pattern)是一种行为设计模式,它通过将对象之间的直接通信转移到一个中介对象中,来减少对象之间的耦合度。这种模式被用来处理一个对象与其他对象之间的交互,使得各对象之间不需要直接相互了解。
29 0