中介者模式(Mediator)Java




中介者模式(Mediator):

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


类图:

中介者模式(Mediator)Java_第1张图片


参考

以上不太好理解:

我在网上找了两幅图帮助理解


中介者模式(Mediator)Java_第2张图片




中介者模式(Mediator)Java_第3张图片





代码


Country国家抽象类


package ding.study.designpatterns.mediator;

/**
 * 国家抽象类
 * 
 * @author daniel
 * 
 */
abstract class Country {
    //联合机构
 protected UnitedNations mediator;

 public Country(UnitedNations mediator) {
  this.mediator = mediator;
 }
}


UnitedNations联合国机构抽象类

package ding.study.designpatterns.mediator;

/**
 * 联合国机构抽象类
 * @author daniel
 *
 */
abstract class UnitedNations {
 //声明
 public abstract void declare(String message,Country colleague);
}

UnitedNationsSecurityCouncil安理会


package ding.study.designpatterns.mediator;
/**
 * 安理会
 * @author daniel
 * @email [email protected]
 * @time 2016-6-12 上午10:07:14
 */
public class UnitedNationsSecurityCouncil extends UnitedNations {
	// 美国
	private USA colleague1;
	// 伊拉克
	private Iraq colleague2;

	/**
	 * @param colleague1
	 *            the colleague1 to set
	 */
	public void setColleague1(USA colleague1) {
		this.colleague1 = colleague1;
	}

	/**
	 * @param colleague2
	 *            the colleague2 to set
	 */
	public void setColleague2(Iraq colleague2) {
		this.colleague2 = colleague2;
	}

	@Override
	public void declare(String message, Country colleague) {
 		if (colleague == colleague1) {
			colleague2.getMessage(message);
		} else {
			colleague1.getMessage(message);
		}

	}

}

USA

package ding.study.designpatterns.mediator;
/**
 * 美国
 * @author daniel
 * @email [email protected]
 * @time 2016-6-12 上午10:03:41
 */
public class USA extends Country {
	 /**
	  * 构造函数
	  * @param mediator
	  */
	 public USA(UnitedNations mediator){
	  //调用父类构造函数
	  super(mediator);
	 }
	 /**
	  * 声明
	  * @param message
	  */
	 public void declare(String message){
	  mediator.declare(message, this);
	 }
	 /**
	  * 获得消息
	  * @param message
	  */
	 public void getMessage(String message){
	  System.out.println("美国获得对方信息:"+message);
	 }

	}



Iraq

package ding.study.designpatterns.mediator;
/**
 * 伊拉克 国家
 * @author daniel
 * @email [email protected]
 * @time 2016-6-12 上午10:03:28
 */
public class Iraq extends Country {
	 /**
	  * 构造函数
	  * @param mediator
	  */
	 public Iraq(UnitedNations mediator){
	  //调用父类构造函数
	  super(mediator);
	 }
	 /**
	  * 声明
	  * @param message
	  */
	 public void declare(String message){
	  mediator.declare(message, this);
	 }
	 /**
	  * 获得消息
	  * @param message
	  */
	 public void getMessage(String message){
	  System.out.println("美国获得对方信息:"+message);
	 }
	}


测试类ZMain


package ding.study.designpatterns.mediator;
/**
 * 中介者模式(Mediator):用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立的改变他们之间的交互。
 * 
 * 输出
 * @author daniel
 * @email [email protected]
 * @time 2016-6-12 下午12:27:40
 */
public class ZMain {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
 
		UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil();
		USA c1 = new USA(UNSC);
		Iraq c2 = new Iraq(UNSC);
		UNSC.setColleague1(c1);
		UNSC.setColleague2(c2);

		c1.declare("美国说 我要吃饭");
		c2.declare("伊拉克说 没饭吃喽");

	}
}


输出结果

中介者模式(Mediator)Java_第4张图片



美国获得对方信息:美国说 我要吃饭
美国获得对方信息:伊拉克说 没饭吃喽


源码 

https://github.com/dingsai88/StudyTest/tree/master/src/ding/study/designpatterns/mediator






你可能感兴趣的:(中介者模式(Mediator)Java)