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

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

一、介绍

中介者模式(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");
 
    }
}


相关文章
|
设计模式 Java 程序员
【设计模式】【行为型模式】中介者模式(Mediator)
一、入门 什么是中介者模式? 中介者模式(Mediator Pattern)是一种行为设计模式,旨在减少对象之间的直接依赖,通过引入一个中介者对象来协调多个对象之间的交互。这种模式特别适用于对象间存在
374 9
|
设计模式 供应链 安全
【再谈设计模式】中介者模式 - 协调对象间交互的枢纽
中介者模式定义了一个中介对象来封装一组对象之间的交互方式。中介者使得各对象之间不需要显式地相互引用,从而降低了它们之间的耦合度。它通过将对象之间的交互逻辑集中到中介者对象中,使得系统的结构更加清晰,易于维护和扩展。
470 18
【再谈设计模式】中介者模式 - 协调对象间交互的枢纽
|
设计模式 前端开发 Java
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
文章详细介绍了中介者模式(Mediator Pattern),这是一种对象行为型模式,用于封装一系列对象的交互,降低系统耦合度,并简化对象之间的交互关系。通过案例分析、结构图、时序图和代码示例,文章展示了中介者模式的组成部分、实现方式和应用场景,并讨论了其优点、缺点和适用情况。
【十三】设计模式~~~行为型模式~~~中介者模式(Java)
|
设计模式 JavaScript
js设计模式【详解】—— 中介者模式
js设计模式【详解】—— 中介者模式
327 5
|
设计模式 Java
Java设计模式-中介者模式(20)
Java设计模式-中介者模式(20)
286 0
|
设计模式 Java 数据库连接
聊聊Java设计模式-中介者模式
中介者(Mediator)模式指定义了一个单独的中介对象,来封装一组对象之间的交互。即将这组对象之间的交互委派给中介对象,从而来避免对象之间的直接交互。
232 1
聊聊Java设计模式-中介者模式
|
设计模式 Go
[设计模式 Go实现] 行为型~中介者模式
[设计模式 Go实现] 行为型~中介者模式
135 1
|
设计模式 Java
小谈设计模式(26)—中介者模式
小谈设计模式(26)—中介者模式
|
设计模式 前端开发 调度
中介者模式--设计模式
中介者模式--设计模式
286 0
中介者模式--设计模式