用一个中介对象来封装一系统的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而可以独立改变他们之间的效死.
组成部份:
1.中介者
2.具体中介者
3.同事
4.具体同事
组成部份之间的关系:
例子:
1.人
package simpleMediator;
/** *//**
* <ul>
* <li>Title:[Person]</li>
* <li>Description: [人(抽象同事)]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public abstract class Person
{
/** *//** 姓名 */
private String name;
/** *//** 条件 */
private int condition;
/** *//** 中介者(不管男孩还是女孩,都需要知道中介者,通过中介者去找对象) */
private Mediator mediator;
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param name
* @param condition
* @param mediator
*/
public Person(String name, int condition, Mediator mediator)
{
this.name = name;
this.condition = condition;
this.mediator = mediator;
}
/** *//**
* <ul>
* <li>Description:[找对象]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param person
* @return
*/
public abstract boolean findPartner(Person person);
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getCondition()
{
return condition;
}
public void setCondition(int condition)
{
this.condition = condition;
}
public Mediator getMediator()
{
return mediator;
}
public void setMediator(Mediator mediator)
{
this.mediator = mediator;
}
}
2.男孩
package simpleMediator;
public class Boy extends Person
{
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param name
* @param condition
* @param mediator
*/
public Boy(String name, int condition, Mediator mediator)
{
super(name, condition, mediator);
}
/**//*
* (non-Javadoc)
*
* @see simpleMediator.Person#findPartner(simpleMediator.Person)
*/
public boolean findPartner(Person person)
{
System.out.println("boy try to find girl.");
this.getMediator().findPartner(person);
return false;
}
}
3.女孩
package simpleMediator;
public class Girl extends Person
{
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param name
* @param condition
* @param mediator
*/
public Girl(String name, int condition, Mediator mediator)
{
super(name, condition, mediator);
}
/**//*
* (non-Javadoc)
*
* @see simpleMediator.Person#findPartner(simpleMediator.Person)
*/
public boolean findPartner(Person person)
{
System.out.println("girl try to find boy.");
this.getMediator().findPartner(person);
return false;
}
}
4.婚介
package simpleMediator;
/** *//**
* <ul>
* <li>Title:[Mediator]</li>
* <li>Description: [婚姻登记]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public interface Mediator
{
/** *//**
* <ul>
* <li>Description:[男孩登记]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param boy
*/
public void registBoy(Boy boy);
/** *//**
* <ul>
* <li>Description:[女孩登记]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param girl
*/
public void registGirl(Girl girl);
/** *//**
* <ul>
* <li>Description:[介绍对象]</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param person
* @return
*/
public boolean findPartner(Person person);
} 5.具体婚介
package simpleMediator;
/** *//**
* <ul>
* <li>Title:[RedSunMediator]</li>
* <li>Description: [红日婚介所]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-3]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class RedSunMediator implements Mediator
{
/** *//** 男孩 */
private Boy boy;
/** *//** 女孩 */
private Girl girl;
/**//* (non-Javadoc)
* @see simpleMediator.Mediator#registBoy(simpleMediator.Boy)
*/
public void registBoy(Boy boy)
{
this.boy = boy;
}
/**//* (non-Javadoc)
* @see simpleMediator.Mediator#registGirl(simpleMediator.Girl)
*/
public void registGirl(Girl girl)
{
this.girl = girl;
}
// 根据各自的条件匹配
public boolean findPartner(Person person)
{
if (person instanceof Boy)
{
this.boy = (Boy) person;
}
else
{
this.girl = (Girl) person;
}
boolean success = this.boy.getCondition() == this.girl.getCondition();
StringBuffer info = new StringBuffer();
info.append(this.boy.getName());
info.append(" vs ");
info.append(this.girl.getName());
info.append(" is partner: " );
info.append(success);
System.out.println(info);
return success;
}
}
6.测试
package simpleMediator;
public class Test
{
public static void main(String[] args)
{
// 红日婚介所开张
Mediator mediator = new RedSunMediator();
// 一个名为小赵的男孩,条件为2500
Boy xiaoZhao = new Boy("小赵", 2500, mediator);
mediator.registBoy(xiaoZhao);
// 一个名为小钱的女孩,条件为为2500
Girl xiaoQian = new Girl("小钱", 2500, mediator);
mediator.registGirl(xiaoQian);
// 如果条件符合,则partner成立
xiaoZhao.findPartner(xiaoQian);
// 一个名为lanlan的女孩,条件为90
Girl xiaosun = new Girl("小孙", 5000, mediator);
mediator.registGirl(xiaosun);
xiaosun.findPartner(xiaoZhao);
}
}
总结
中介者模式很容易在系统中应用,也很容易在系统中误用。当系统出现了“多对多”交互复杂的对象群,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。