编程语言 php java

Java设计模式-中介者模式

java HTML我帮您 1年前  0次浏览
中介者模式(Mediator Pattern)中介者模式用于减少多个对象和类之间的通信复杂度。这种设计模式提供一个中介者类处理所有不同类之间的通信,并且减少类之间的耦合性,使得代码更易维护。中介者模式属于表现型。

如果一个对象的操作会引起其他相关对象的变化,或者是某个操作需要引起其他对象的后续或连带操作,而这个对象又不希望自己来处理这些关系,那么久可以找中介者,把所有麻烦扔给它,只要在需要的时候通知中介者,其他的就让中介者去处理就可以了。

反过来,其他对象在操作时,可能会引起这个对象的变化,也可以这么做。最后对象之间就完全分离了,谁都不直接跟其他对象交互,那么相互之间的关系完全集中到中介者里面了,所有的对象都通过中介者进行通信,相互之间就不在有联系了,对象之间的耦合就松散了。

● 标准的中介者模式

Mediator :中介者接口。在里面定义各个同事对象之间的交互对象,可以是公共的通信方法,比如sendMsg方法,大家都用,也可以是小范围的交互方法。

ConcreteMediator :具体中介者实现对象。他需要维护各个同事对象之间的交互关系,比如下例中的ConcreteMediator。

Colleague :同事类的定义,所有具体同事类的父类,通常实现成抽象类,主要负责约束同事对象的类型,并实现一些具体同事类之间的功能。

ConcreteColleague :具体的同事类,实现自己的业务,在需要与其他同事通信的时候,就与持有的中介者通信,中介者负责与其他的同事进行交互。比如下例中的Colleague1,Colleague2,Colleague3
package com.html580.mediator;

import java.util.ArrayList;
import java.util.List;

abstract class Colleague {
// 引入中介者
private Mediator mediator;

public Mediator getMediator() {
return mediator;
}

public Colleague(Mediator m) {
mediator = m;
}

// 消息
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

// 发送消息
public abstract void sendMsg();

// 收到消息
public abstract void getMsg(String msg);

// 发送消息
public void sendMsg(String msg) {
this.message = msg;
mediator.action(this);
}
}

class Colleague1 extends Colleague {

public Colleague1(Mediator m) {
super(m);
}

public void getMsg(String msg) {
System.out.println("Colleague1 has got the message -'" + msg + "'");
}

public void sendMsg() {
System.out.println("Colleague1 has send the message '" + getMessage() + "'");
}
}

class Colleague2 extends Colleague {

public Colleague2(Mediator m) {
super(m);
}

public void getMsg(String msg) {
System.out.println("Colleague2 has got the message -'" + msg + "'");
}

public void sendMsg() {
System.out.println("Colleague2 has send the message '" + getMessage()
+ "'");
}
}

class Colleague3 extends Colleague {

public Colleague3(Mediator m) {
super(m);
}

public void getMsg(String msg) {
System.out.println("Colleague3 has got the message -'" + msg + "'");
}

public void sendMsg() {
System.out.println("Colleague3 has send the message '" + getMessage()
+ "'");
}
}

// 中介者
abstract class Mediator {
// Mediator针对Colleague的一个交互行为
public abstract void action(Colleague sender);

// 加入Colleague对象
public abstract void addCollegue(Colleague colleague);
}

class ConcreteMediator extends Mediator {

private List colleagues = new ArrayList(0);

public void addCollegue(Colleague colleague) {
colleagues.add(colleague);
}

public void action(Colleague actor) {
String msg = actor.getMessage();
for (Colleague colleague : colleagues) {
if (colleague.equals(actor)) {
colleague.sendMsg();
break;
}
}
for (Colleague colleague : colleagues) {
if (colleague.equals(actor))
continue;
else
colleague.getMsg(msg);
}
}
}

// 测试类
public class Client {

public static void main(String<> args) {
// 生成中介者 并注入到各个Colleague对象中
Mediator mediator = new ConcreteMediator();
Colleague colleague1 = new Colleague1(mediator);
Colleague colleague2 = new Colleague2(mediator);
Colleague colleague3 = new Colleague3(mediator);

// 注册对象到中介
mediator.addCollegue(colleague1);
mediator.addCollegue(colleague2);
mediator.addCollegue(colleague3);

// Colleague1 触发行为
colleague1.sendMsg("嗨,大家好,欢迎来到HTML我帮您QQ论坛!");
System.out.println();
// Colleague2 触发行为
colleague2.sendMsg("很高兴见能加入此论坛!");
System.out.println();
// Colleague3 触发行为
colleague3.sendMsg("我们一起来维护论坛吧!");
System.out.println();
}
}
输出结果:
Colleague1 has send the message '嗨,大家好,欢迎来到HTML我帮您QQ论坛!'
Colleague2 has got the message -'嗨,大家好,欢迎来到HTML我帮您QQ论坛!'
Colleague3 has got the message -'嗨,大家好,欢迎来到HTML我帮您QQ论坛!'

Colleague2 has send the message '很高兴见能加入此论坛!'
Colleague1 has got the message -'很高兴见能加入此论坛!'
Colleague3 has got the message -'很高兴见能加入此论坛!'

Colleague3 has send the message '我们一起来维护论坛吧!'
Colleague1 has got the message -'我们一起来维护论坛吧!'
Colleague2 has got the message -'我们一起来维护论坛吧!'




发表评论