设计模式之中介者模式(Mediator Pattern)

概述

If two or more objects need to cooperate, the Mediator pattern might be applied. Especially if the objects do not know each other, if they should not be tightly coupled, or their interaction is complex, this pattern can help.

Mediator模式是指假如有多个对象存在交互,我们可以为了方便,可以创建一个Mediator对象来帮助他们进行交互。这样可以解耦因为对象要互相交互,导致的高耦合。

例如:我们根据不同类型的学生选择不同类型的房子,那么为了避免不同学生和不同类型房子之间的高耦合引用,我们使用了Mediator做为中介,这个中介负责学生和房子的交流活动。

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

本文类图

本文类图

本文代码

产品类
学生类
Mediator负责学生和产品之间的交流

参考资料

参考文档,参考代码,本文githut代码地址

你可能感兴趣的:(设计模式之中介者模式(Mediator Pattern))