设计模式系列—中介者模式

开发 前端
本篇和大家一起来学习中介者模式相关内容。

模式定义

定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。中介者模式又叫调停模式,它是迪米特法则的典型应用。

 

  • 迪米特法则(Law of Demeter,LoD)又叫作最少知识原则(Least Knowledge Principle,LKP),产生于 1987 年美国东北大学(Northeastern University)的一个名为迪米特(Demeter)的研究项目,由伊恩·荷兰(Ian Holland)提出,被 UML 创始者之一的布奇(Booch)普及,后来又因为在经典著作《程序员修炼之道》(The Pragmatic Programmer)提及而广为人知。
  • 迪米特法则的定义是:只与你的直接朋友交谈,不跟“陌生人”说话(Talk only to your immediate friends and not to strangers)。其含义是:如果两个软件实体无须直接通信,那么就不应当发生直接的相互调用,可以通过第三方转发该调用。其目的是降低类之间的耦合度,提高模块的相对独立性。
  • 迪米特法则中的“朋友”是指:当前对象本身、当前对象的成员对象、当前对象所创建的对象、当前对象的方法参数等,这些对象同当前对象存在关联、聚合或组合关系,可以直接访问这些对象的方法。

模板实现如下:

package com.niuh.designpattern.mediator.v1; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * <p> 
 * 中介者模式 
 * </p> 
 */ 
public class MediatorPattern { 
 
    public static void main(String[] args) { 
        Mediator md = new ConcreteMediator(); 
        Colleague c1, c2; 
        c1 = new ConcreteColleague1(); 
        c2 = new ConcreteColleague2(); 
        md.register(c1); 
        md.register(c2); 
        c1.send(); 
        System.out.println("=============="); 
        c2.send(); 
    } 

 
//抽象中介者 
abstract class Mediator { 
    public abstract void register(Colleague colleague); 
 
    public abstract void relay(Colleague cl); //转发 

 
//具体中介者 
class ConcreteMediator extends Mediator { 
    private List<Colleague> colleagues = new ArrayList<Colleague>(); 
 
    public void register(Colleague colleague) { 
        if (!colleagues.contains(colleague)) { 
            colleagues.add(colleague); 
            colleague.setMedium(this); 
        } 
    } 
 
    public void relay(Colleague cl) { 
        for (Colleague ob : colleagues) { 
            if (!ob.equals(cl)) { 
                ((Colleague) ob).receive(); 
            } 
        } 
    } 

 
//抽象同事类 
abstract class Colleague { 
    protected Mediator mediator; 
 
    public void setMedium(Mediator mediator) { 
        this.mediator = mediator; 
    } 
 
    public abstract void receive(); 
 
    public abstract void send(); 

 
//具体同事类 
class ConcreteColleague1 extends Colleague { 
    public void receive() { 
        System.out.println("具体同事类1收到请求。"); 
    } 
 
    public void send() { 
        System.out.println("具体同事类1发出请求。"); 
        mediator.relay(this); //请中介者转发 
    } 

 
//具体同事类 
class ConcreteColleague2 extends Colleague { 
    public void receive() { 
        System.out.println("具体同事类2收到请求。"); 
    } 
 
    public void send() { 
        System.out.println("具体同事类2发出请求。"); 
        mediator.relay(this); //请中介者转发 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.

结果实现如下:

  • 具体同事类1发出请求。
  • 具体同事类2收到请求。
  • 具体同事类2发出请求。
  • 具体同事类1收到请求。

解决的问题

对象与对象之间存在大量的关联关系,这样势必会导致系统的结构变得很复杂,同时若一个对象发生改变,我们也需要跟踪与之相关联的对象,同时做出相应的处理。

模式组成

 

中介者模式实现的关键是找出“中介者”。

实例说明

实例概况

用中介者模式编写一个“北京房地产交流平台”程序。

 

分析:北京房地产交流平台是“房地产中介公司”提供给“卖方客户”与“买方客户”进行信息交流的平台,比较适合用中介者模式来实现。

使用步骤

 

步骤1:定义一个中介公司(Medium)接口,它是抽象中介者,它包含了客户注册方法 register(Customer member) 和信息转发方法 relay(String from,String ad);

interface Medium { 
    //客户注册 
    void register(Customer member); 
 
    //转发 
    void relay(String from, String ad); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

步骤2:定义一个北京房地产中介(EstateMedium)公司,它是具体中介者类,它包含了保存客户信息的 List 对象,并实现了中介公司中的抽象方法。

//具体中介者:房地产中介 
class EstateMedium implements Medium { 
    private List<Customer> members = new ArrayList<Customer>(); 
 
    public void register(Customer member) { 
        if (!members.contains(member)) { 
            members.add(member); 
            member.setMedium(this); 
        } 
    } 
 
    public void relay(String from, String ad) { 
        for (Customer ob : members) { 
            String name = ob.getName(); 
            if (!name.equals(from)) { 
                ((Customer) ob).receive(from, ad); 
            } 
        } 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

步骤3:定义一个客户(Qistomer)类,它是抽象同事类,其中包含了中介者的对象,和发送信息的 send(String ad) 方法与接收信息的 receive(String from,Stringad) 方法的接口,由于本程序是窗体程序,所以本类继承 JPmme 类,并实现动作事件的处理方法 actionPerformed(ActionEvent e)。

//抽象同事类:客户 
abstract class Customer extends JFrame implements ActionListener { 
    private static final long serialVersionUID = -7219939540794786080L; 
    protected Medium medium; 
    protected String name
    JTextField SentText; 
    JTextArea ReceiveArea; 
 
    public Customer(String name) { 
        super(name); 
        this.name = name
    } 
 
    void ClientWindow(int x, int y) { 
        Container cp; 
        JScrollPane sp; 
        JPanel p1, p2; 
        cp = this.getContentPane(); 
        SentText = new JTextField(18); 
        ReceiveArea = new JTextArea(10, 18); 
        ReceiveArea.setEditable(false); 
        p1 = new JPanel(); 
        p1.setBorder(BorderFactory.createTitledBorder("接收内容:")); 
        p1.add(ReceiveArea); 
        sp = new JScrollPane(p1); 
        cp.add(sp, BorderLayout.NORTH); 
        p2 = new JPanel(); 
        p2.setBorder(BorderFactory.createTitledBorder("发送内容:")); 
        p2.add(SentText); 
        cp.add(p2, BorderLayout.SOUTH); 
        SentText.addActionListener(this); 
        this.setLocation(x, y); 
        this.setSize(250, 330); 
        this.setResizable(false); //窗口大小不可调整 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.setVisible(true); 
    } 
 
    public void actionPerformed(ActionEvent e) { 
        String tempInfo = SentText.getText().trim(); 
        SentText.setText(""); 
        this.send(tempInfo); 
    } 
 
    public String getName() { 
        return name
    } 
 
    public void setMedium(Medium medium) { 
        this.medium = medium; 
    } 
 
    public abstract void send(String ad); 
 
    public abstract void receive(String from, String ad); 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

步骤4:定义卖方(Seller)类和买方(Buyer)类,它们是具体同事类,是客户(Customer)类的子类,它们实现了父类中的抽象方法,通过中介者类进行信息交流。

//具体同事类:卖方 
class Seller extends Customer { 
    private static final long serialVersionUID = -1443076716629516027L; 
 
    public Seller(String name) { 
        super(name); 
        ClientWindow(50, 100); 
    } 
 
    public void send(String ad) { 
        ReceiveArea.append("我(卖方)说: " + ad + "\n"); 
        //使滚动条滚动到最底端 
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
        medium.relay(name, ad); 
    } 
 
    public void receive(String from, String ad) { 
        ReceiveArea.append(from + "说: " + ad + "\n"); 
        //使滚动条滚动到最底端 
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
    } 

 
//具体同事类:买方 
class Buyer extends Customer { 
    private static final long serialVersionUID = -474879276076308825L; 
 
    public Buyer(String name) { 
        super(name); 
        ClientWindow(350, 100); 
    } 
 
    public void send(String ad) { 
        ReceiveArea.append("我(买方)说: " + ad + "\n"); 
        //使滚动条滚动到最底端 
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
        medium.relay(name, ad); 
    } 
 
    public void receive(String from, String ad) { 
        ReceiveArea.append(from + "说: " + ad + "\n"); 
        //使滚动条滚动到最底端 
        ReceiveArea.setCaretPosition(ReceiveArea.getText().length()); 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

输出结果

优点

  1. 降低了对象之间的耦合性,使得对象易于独立地被复用。
  2. 将对象间的一对多关联转变为一对一的关联,提高系统的灵活性,使得系统易于维护和扩展。

缺点

当同事类太多时,中介者的职责将很大,它会变得复杂而庞大,以至于系统难以维护。

应用场景

  • 当对象之间存在复杂的网状结构关系而导致依赖关系混乱且难以复用时。
  • 当想创建一个运行于多个类之间的对象,又不想生成新的子类时。

模式的扩展

在实际开发中,通常采用以下两种方法来简化中介者模式,使开发变得更简单。

 

  1. 不定义中介者接口,把具体中介者对象实现成为单例。
  2. 同事对象不持有中介者,而是在需要的时候直接获取中介者对象并调用。

程序代码如下:

package com.niuh.designpattern.mediator.v3; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * <p> 
 * 简化中介者模式 
 * </p> 
 */ 
public class SimpleMediatorPattern { 
    public static void main(String[] args) { 
        SimpleColleague c1, c2; 
        c1 = new SimpleConcreteColleague1(); 
        c2 = new SimpleConcreteColleague2(); 
        c1.send(); 
        System.out.println("=============="); 
        c2.send(); 
    } 

 
//简单单例中介者 
class SimpleMediator { 
    private static SimpleMediator smd = new SimpleMediator(); 
    private List<SimpleColleague> colleagues = new ArrayList<SimpleColleague>(); 
 
    private SimpleMediator() { 
    } 
 
    public static SimpleMediator getMedium() { 
        return (smd); 
    } 
 
    public void register(SimpleColleague colleague) { 
        if (!colleagues.contains(colleague)) { 
            colleagues.add(colleague); 
        } 
    } 
 
    public void relay(SimpleColleague scl) { 
        for (SimpleColleague ob : colleagues) { 
            if (!ob.equals(scl)) { 
                ((SimpleColleague) ob).receive(); 
            } 
        } 
    } 

 
//抽象同事类 
interface SimpleColleague { 
    void receive(); 
 
    void send(); 

 
//具体同事类 
class SimpleConcreteColleague1 implements SimpleColleague { 
    SimpleConcreteColleague1() { 
        SimpleMediator smd = SimpleMediator.getMedium(); 
        smd.register(this); 
    } 
 
    public void receive() { 
        System.out.println("具体同事类1:收到请求。"); 
    } 
 
    public void send() { 
        SimpleMediator smd = SimpleMediator.getMedium(); 
        System.out.println("具体同事类1:发出请求..."); 
        smd.relay(this); //请中介者转发 
    } 

 
//具体同事类 
class SimpleConcreteColleague2 implements SimpleColleague { 
    SimpleConcreteColleague2() { 
        SimpleMediator smd = SimpleMediator.getMedium(); 
        smd.register(this); 
    } 
 
    public void receive() { 
        System.out.println("具体同事类2:收到请求。"); 
    } 
 
    public void send() { 
        SimpleMediator smd = SimpleMediator.getMedium(); 
        System.out.println("具体同事类2:发出请求..."); 
        smd.relay(this); //请中介者转发 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.

输出结果如下:

  • 具体同事类1:发出请求...
  • 具体同事类2:收到请求。
  • 具体同事类2:发出请求...
  • 具体同事类1:收到请求。

 

源码中的应用

java.util.Timer 
java.util.concurrent.Executer#execute() 
java.util.concurrent.ExecuterService#submit() 
java.lang.reflect.Method#invoke() 
  • 1.
  • 2.
  • 3.
  • 4.

PS:以上代码提交在 Github :

https://github.com/Niuh-Study/niuh-designpatterns.git

 

责任编辑:姜华 来源: 今日头条
相关推荐

2023-05-26 08:41:23

模式Go设计模式

2021-07-20 08:52:20

命令模式中介者模式设计模式

2021-10-26 00:21:19

设计模式建造者

2020-10-20 13:33:00

建造者模式

2020-10-26 08:45:39

观察者模式

2021-01-21 05:34:14

设计模式建造者

2021-07-08 11:28:43

观察者模式设计

2013-11-26 17:09:57

Android设计模式

2023-10-07 00:17:06

AirDrop中介者模式

2022-01-29 22:12:35

前端模式观察者

2024-06-05 09:41:41

2020-10-23 09:40:26

设计模式

2020-11-03 13:05:18

命令模式

2020-11-04 08:54:54

状态模式

2022-01-12 13:33:25

工厂模式设计

2020-10-28 11:56:47

桥接模式

2021-10-28 19:09:09

模式原型Java

2020-11-09 08:20:33

解释器模式

2012-01-13 15:59:07

2021-06-09 08:53:34

设计模式策略模式工厂模式
点赞
收藏

51CTO技术栈公众号