中介者模式——解决“依赖关系结构混乱”问题

目录

一、基础简介

1、定义

2、使用场景

3、优缺点

4、模式结构分析 

二、实例实现

1、实例场景

2、房屋中介:抽象中介者(Mediator)角色

3、链家:具体中介者(ConcreteMediator)角色:维护同事的交互关系

4、链家的顾客:抽象同事类(Colleague)角色

5、委托链家卖房和找链家买房的顾客:具体同事类(Concrete Colleague)角色

6、客户端类

7、结果展示


一、基础简介

1、定义

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

2、使用场景

  •  1、系统中对象之间存在比较复杂的引用关系,导致它们之间的依赖关系结构混乱而且难以复用该对象
  • 2、想通过一个中间类来封装多个类中的行为,而又不想生成太多的子类。

3、优缺点

优点: 1、降低了类的复杂度,将一对多转化成了一对一。 2、各个类之间的解耦。 3、符合迪米特原则。

缺点:中介者会庞大,变得复杂难以维护。

4、模式结构分析 

  • 抽象中介者(Mediator)角色:它是中介者的接口,提供了同事对象注册与转发同事对象信息的抽象方法。
  • 具体中介者(ConcreteMediator)角色:实现中介者接口,定义一个 List 来管理同事对象,协调各个同事角色之间的交互关系,因此它依赖于同事角色。
  • 抽象同事类(Colleague)角色:定义同事类的接口,保存中介者对象,提供同事对象交互的抽象方法,实现所有相互影响的同事类的公共功能。
  • 具体同事类(Concrete Colleague)角色:是抽象同事类的实现者,当需要与其他同事对象交互时,由中介者对象负责后续的交互。

二、实例实现

1、实例场景

就比如说现在的“房屋中介”,就是“中介者模式”的一个很好的实践:

  • “房屋中介”就是Mediator,抽象的中介者
  • “链家”就是ConcreteMediator,具体的中介者对象
  • 而“链家的顾客”就是Colleague,抽象的同事类
  • “具体的买房者”和“卖房者”就是ConcreteColleague,具体同事类

2、房屋中介:抽象中介者(Mediator)角色

package com.mfc.design.中介者模式;

/**
 * @author MouFangCai
 * @date 2019/10/24 17:21
 *
 * @description 房屋中介:抽象中介者(Mediator)角色
 */
public interface Mediator {

    // 定义一个抽象的发消息方法,得到消息内容和同事对象
    void send(String message, Colleague sender);
}

3、链家:具体中介者(ConcreteMediator)角色:维护同事的交互关系

package com.mfc.design.中介者模式;

import lombok.Data;

/**
 * @author MouFangCai
 * @date 2019/10/24 17:24
 *
 * @description 链家:具体中介者(ConcreteMediator)角色
 */
@Data
public class ConcreteMediator implements Mediator{

    private Buyer_ConcreteColleague buyer;
    private Seller_ConcreteColleague seller;

    // 具体中介者维护 同事间的交互
    @Override
    public void send(String message, Colleague sender) {
        // buyer发消息给seller
        if (sender instanceof Buyer_ConcreteColleague) {
            // seller 收到消息
            System.out.println("Buyer发消息给你了:");
            seller.notify(message);
            System.out.println();
        } else {
            System.out.println("Seller发消息给你了:");
            buyer.notify(message);
            System.out.println();
        }
    }
}

4、链家的顾客:抽象同事类(Colleague)角色

package com.mfc.design.中介者模式;

/**
 * @author MouFangCai
 * @date 2019/10/24 17:22
 * @description 链家的顾客:抽象同事类(Colleague)角色
 */
public abstract class Colleague {

    protected Mediator mediator;

    // 得到中介者对象
    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }
}

5、委托链家卖房和找链家买房的顾客:具体同事类(Concrete Colleague)角色

package com.mfc.design.中介者模式;

/**
 * @author MouFangCai
 * @date 2019/10/24 17:23
 *
 * @description 具体同事类(Concrete Colleague)角色
 */
public class Buyer_ConcreteColleague extends Colleague{

    public Buyer_ConcreteColleague(Mediator mediator) {
        super(mediator);
    }

    // 通过中介者 发消息
    public void send(String message) {
        mediator.send(message,this);
    }

    public void notify(String message) {
        System.out.println("Buyer收到消息:" + message);
    }

}
package com.mfc.design.中介者模式;

/**
 * @author MouFangCai
 * @date 2019/10/24 17:23
 *
 * @description 具体同事类(Concrete Colleague)角色
 */
public class Seller_ConcreteColleague extends Colleague{

    public Seller_ConcreteColleague(Mediator mediator) {
        super(mediator);
    }

    // 通过中介者 发消息
    public void send(String message) {
        mediator.send(message,this);
    }

    public void notify(String message) {
        System.out.println("seller收到消息:" + message);
    }

}

6、客户端类

package com.mfc.design.中介者模式;

/**
 * @author MouFangCai
 * @date 2019/10/24 17:54
 * @description
 */
public class Client {
    public static void main(String[] args) {
        // 链家:中介
        ConcreteMediator mediator = new ConcreteMediator();
        // 让两个客户都认识中介
        Buyer_ConcreteColleague buyer = new Buyer_ConcreteColleague(mediator);
        Seller_ConcreteColleague seller = new Seller_ConcreteColleague(mediator);
        // 让中介都认识两个同事
        mediator.setBuyer(buyer);
        mediator.setSeller(seller);
        // 同事类对象互通消息,都是通过中介转发的
        buyer.send("这个房子100万,卖不");
        seller.send("100万太低了,至少得110万");
    }
}

7、结果展示

Buyer发消息给你了:
seller收到消息:这个房子100万,卖不

Seller发消息给你了:
Buyer收到消息:100万太低了,至少得110万


Process finished with exit code 0

发布了101 篇原创文章 · 获赞 176 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_36095679/article/details/102726861