您的位置:首页 > 编程语言 > Java开发

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

2017-03-17 20:25 573 查看
意图:
用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互.

适用性:
1.一组对象以定义良好但是复杂的方式进行通信.产生的相互依赖关系结构混乱且难以理解.
2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象
3.想定制一个分布在多个类中的行为,而又不想生成太多的子类.

效果:
1.减少了子类生成
2.它将各Colleague解耦
3.它简化了对象协议
4.它对对象如何协作进行了抽象
5.它使控制中心话

代码实现:
package com.git.books.b_design_patterns.q_medlator;
/**
* @Description:协议中介者
* @author: songqinghu
* @date: 2017年3月17日 下午7:25:01
* Version:1.0
*/
public abstract class AgreementMedlator {

/**
* @描述:国家同事改变时触发具体处理
* @param colleague
* @return void
* @createTime:2017年3月17日
* @author: songqinghu
*/
public abstract void countryColleagueChanged(Colleague colleague,Action action);

}
package com.git.books.b_design_patterns.q_medlator;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
* @Description: WTO国际贸易协议
* @author: songqinghu
* @date: 2017年3月17日 下午7:58:12
* Version:1.0
*/
public class WTOAgreementMedlator extends AgreementMedlator {

private  Set<Colleague> colleagues = new HashSet<Colleague>();

@Override
public void countryColleagueChanged(Colleague colleague, Action action) {
if(Action.Expand.equals(action)){
colleagues.add(colleague);
}else if(Action.Fade.equals(action)){
colleagues.remove(colleague);
}

for (Colleague c : colleagues) {
c.action(action);
}
}

}

package com.git.books.b_design_patterns.q_medlator;

public abstract class Colleague {

private AgreementMedlator medlator;

public Colleague(AgreementMedlator medlator) {
this.medlator = medlator;
}
/**
* @描述:子类行动方法
* @return void
* @exception
* @createTime:2017年3月17日
* @author: songqinghu
*/
public abstract void action(Action action);
/**
* @描述:子类改变方法
* @return void
* @createTime:2017年3月17日
* @author: songqinghu
*/
public abstract void change(Change change);

/**
* @描述:返回当前协议对象
* @return AgreementMedlator
* @createTime:2017年3月17日
* @author: songqinghu
*/
public  AgreementMedlator getMedlator(){
return this.medlator;
}

}

/**
* @Description: 改变操作
* @author: songqinghu
* @date: 2017年3月17日 下午7:40:24
* Version:1.0
*/
enum Change{
Join,SignOut,Activity
}
/**
* @Description: 具体行动指令
* @author: songqinghu
* @date: 2017年3月17日 下午7:43:22
* Version:1.0
*/
enum Action{
Expand,Fade,Exchange
}

package com.git.books.b_design_patterns.q_medlator;
/**
* @Description: 中国伙伴
* @author: songqinghu
* @date: 2017年3月17日 下午7:37:58
* Version:1.0
*/
public class ChinaColleague extends Colleague {

private String name ="china";

public ChinaColleague(AgreementMedlator medlator) {
super(medlator);
}

@Override
public void action(Action action) {
//can create factroy process this action
if(Action.Expand.equals(action)){
System.out.println("the " + name + " expanding !");
}else if(Action.Fade.equals(action)){
System.out.println("the " + name + " backward !");
}else{
System.out.println("the " + name + " get some thing!");
}
}

@Override
public void change(Change change) {
if(change ==null)
throw new RuntimeException("the change can't null ");
Action action ;
if(Change.Join.equals(change)){
action = Action.Expand;
}else if(Change.SignOut.equals(change)){
action = Action.Fade;
}else{
action = Action.Exchange;
}
this.getMedlator().countryColleagueChanged(this,action);
}

}

package com.git.books.b_design_patterns.q_medlator;
/**
* @Description: 中国伙伴
* @author: songqinghu
* @date: 2017年3月17日 下午7:37:58
* Version:1.0
*/
public class AmericaColleague extends Colleague {

private String name ="america";

public AmericaColleague(AgreementMedlator medlator) {
super(medlator);
}

@Override
public void action(Action action) {
//can create factroy process this action
if(Action.Expand.equals(action)){
System.out.println("the " + name + " expanding !");
}else if(Action.Fade.equals(action)){
System.out.println("the " + name + " backward !");
}else{
System.out.println("the  " + name + "  get some thing!");
}
}

@Override
public void change(Change change) {
if(change ==null)
throw new RuntimeException("the change can't null ");
Action action ;
if(Change.Join.equals(change)){
action = Action.Expand;
}else if(Change.SignOut.equals(change)){
action = Action.Fade;
}else{
action = Action.Exchange;
}
this.getMedlator().countryColleagueChanged(this,action);
}

}

package com.git.books.b_design_patterns.q_medlator;
/**
* @Description: 中介者模式测试类
* @author: songqinghu
* @date: 2017年3月17日 下午8:03:19
* Version:1.0
*/
public class MedlatorClient {

public static void main(String[] args) {

AgreementMedlator medlator = new WTOAgreementMedlator();

Colleague china = new ChinaColleague(medlator);

Colleague america = new AmericaColleague(medlator);

china.change(Change.Join);

america.change(Change.Join);

china.change(Change.Activity);

america.change(Change.SignOut);

}

}

结构图:



重点:
理解中介者模式,合理使用此模式

参考:
<<设计模式>>
<<Java与模式>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息