中介者模式Mediator
场景:公司中各个部门需要交互,通过中介总经理进行交互
核心:
如果一个系统中对象之间的联系成网状结构,对象之间多对多,将导致关系极其复杂,这些对象统称为“同事关系”
我们可以引入一个中介者对象,使各个同事对象只和自身与中介者打交道,中介再去其他部门交互
中介者模式的本质:
解耦多个同事对象之间的交互关系,每个对象中都持有中介者
开发中常见场景:
-MVC模式(其中的C,控制器就是一个中介者对象。M和V都和它打交道)
-窗口游戏程序,窗口软件开发中窗口也是一个中介者
-GUI中,多个组件之间的交互
-Java.lang.reflect.Method#invoke()
与代理模式的不同理解:代理模式是客户端的中介,中介者模式是同事之间的中介
例子:各个部门和总经理
/**
* 中介者接口
* @author 小帆敲代码
*
*/
public interface Mediator {
void register(String dname,Department d);
void command(String dname);
}
/**
* 具体中介者(总经理)
* @author 小帆敲代码
*
*/
class President implements Mediator{
//同事集合
private Map<String,Department> map=new HashMap<>();
@Override
public void register(String dname, Department d) {
map.put(dname, d);
}
* 中介者接口
* @author 小帆敲代码
*
*/
public interface Mediator {
void register(String dname,Department d);
void command(String dname);
}
/**
* 具体中介者(总经理)
* @author 小帆敲代码
*
*/
class President implements Mediator{
//同事集合
private Map<String,Department> map=new HashMap<>();
@Override
public void register(String dname, Department d) {
map.put(dname, d);
}
//对其他部门的交互
@Override
public void command(String dname) {
map.get(dname).selfAction();
}
}
@Override
public void command(String dname) {
map.get(dname).selfAction();
}
}
/**
* 同事关系接口
* @author 小帆敲代码
*
*/
public interface Department {
void selfAction();//自己部门的活动
void outAction(String dname);//与别的部门的交互
}
/**
* 具体同事类(部门)
* @author 小帆敲代码
*
*/
class Development implements Department{
private Mediator m;
public Development(Mediator m) {
this.m = m;
m.register("development", this);
}
@Override
public void selfAction() {
System.out.println("专心科研,开发项目!");
}
@Override
public void outAction(String dname) {
m.command(dname);
}
}
class Market implements Department{
private Mediator m;
public Market(Mediator m) {
this.m = m;
m.register("market", this);
}
@Override
public void selfAction() {
System.out.println("市场调研,接项目!");
}
@Override
public void outAction(String dname) {
m.command(dname);
}
}
class Finicial implements Department{
private Mediator m;
public Finicial(Mediator m) {
this.m = m;
m.register("finicial", this);
}
@Override
public void selfAction() {
System.out.println("发钱!");
}
@Override
public void outAction(String dname) {
m.command(dname);
}
}
* 同事关系接口
* @author 小帆敲代码
*
*/
public interface Department {
void selfAction();//自己部门的活动
void outAction(String dname);//与别的部门的交互
}
/**
* 具体同事类(部门)
* @author 小帆敲代码
*
*/
class Development implements Department{
private Mediator m;
public Development(Mediator m) {
this.m = m;
m.register("development", this);
}
@Override
public void selfAction() {
System.out.println("专心科研,开发项目!");
}
@Override
public void outAction(String dname) {
m.command(dname);
}
}
class Market implements Department{
private Mediator m;
public Market(Mediator m) {
this.m = m;
m.register("market", this);
}
@Override
public void selfAction() {
System.out.println("市场调研,接项目!");
}
@Override
public void outAction(String dname) {
m.command(dname);
}
}
class Finicial implements Department{
private Mediator m;
public Finicial(Mediator m) {
this.m = m;
m.register("finicial", this);
}
@Override
public void selfAction() {
System.out.println("发钱!");
}
@Override
public void outAction(String dname) {
m.command(dname);
}
}
/**
* 客户端
* @author 小帆敲代码
*
*/
public class Client {
public static void main(String[] args) {
Mediator m=new President();
Development devp=new Development(m);
Market market=new Market(m);
Finicial finicial=new Finicial(m);
devp.selfAction();
devp.outAction("finicial");
}
}
* 客户端
* @author 小帆敲代码
*
*/
public class Client {
public static void main(String[] args) {
Mediator m=new President();
Development devp=new Development(m);
Market market=new Market(m);
Finicial finicial=new Finicial(m);
devp.selfAction();
devp.outAction("finicial");
}
}