OOD鼓励奖行为封装到各个对象中,也就是将变化点封装。但是有时候这种封装可能会导致对象间有许多连接。极端情况下,每个对象都知道其他所有对象。各个对象之间的交互比较多,每个对象的行为操作都依赖其他对象。修改一个对象的行为,很可能同时涉及到修改很多其他对象的行为。那么这时,可以使用Mediator模式将集体行为封装在一个“中介者对象中以避免这个问题。使用Mediator模式可以使各个对象间的耦合松散,只要关心每个对象和Mediator的关系,这样使多对多的关系变成了一对多的关系,每个对象都和Mediator联系。降低了系统得复杂性,提高了可扩展性和可修改性。
下面是实例代码:
package Mediator;
public abstract class Mediator
{
public abstract void register(Colleague c, String type);
public abstract void changed(String type);
}//end class Mediator
package Mediator;
public class ConcreteMediator extends Mediator
{
private ColleagueA a;
private ColleagueB b;
private ColleagueC c;
public ConcreteMediator()
{
//do something.
}//end ConcreteMediator()
public void register(Colleague colleague, String str)
{
if(str.equals("A")) {
a = (ColleagueA)colleague;
} else if (str.equals("B")) {
b = (ColleagueB)colleague;
} else if (str.equals("C")) {
c = (ColleagueC)colleague;
}
}//end register()
public void changed(String type)
{
if(type.equals("A")) {
b.action();
c.action();
} else if (type.equals("B")) {
a.action();
c.action();
} else if (type.equals("C")) {
a.action();
b.action();
}
}//end change()
}//end class changed()
package Mediator;
public abstract class Colleague
{
public abstract void change();
public abstract void action();
}//end class Colleague
package Mediator;
public class ColleagueA extends Colleague
{
private final String type = "A";
private Mediator med;
public ColleagueA(Mediator m)
{
med = m;
med.register(this, type);
}//end ColleagueA()
public void change()
{
System.out.println("---------- A changed now ! ----------");
med.changed(type);
}//end change()
public void action()
{
System.out.println(" A is changed by mediator ");
}//end action()
}//end class Colleague
package Mediator;
public class ColleagueB extends Colleague
{
private final String type = "B";
private Mediator med;
public ColleagueB(Mediator m)
{
med = m;
med.register(this, type);
}//end ColleagueB()
public void change()
{
System.out.println("----- B changed now ! -----");
med.changed(type);
}//end change()
public void action()
{
System.out.println(" B is changed by mediator ");
}//end action()
}//end class ColleagueB
package Mediator;
public class ColleagueC extends Colleague
{
private final String type = "C";
private Mediator med;
public ColleagueC(Mediator m)
{
med = m;
med.register(this, type);
}//end ColleagueC()
public void change()
{
System.out.println("----- C changed now ! -----");
med.changed(type);
}//end change()
public void action()
{
System.out.println(" C is changed by mediator ");
}//end action()
}//end class ColleagueC
/**//*
* MediatorPattern.java
*
* Created on 2006年5月4日, 下午3:47
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Mediator;
/** *//**
*
* @author Administrator
*/
public class MediatorPattern
{
Mediator myMed = new ConcreteMediator();
ColleagueA ca = new ColleagueA(myMed);
ColleagueB cb = new ColleagueB(myMed);
ColleagueC cc = new ColleagueC(myMed);
/** *//** Creates a new instance of MediatorPattern */
public MediatorPattern()
{
//do something.
}//end MediatorPattern()
public void showMediatorPattern()
{
ca.change();
cb.change();
cc.change();
}//end showMediatorPattern()
public static void main(String[] args)
{
StringBuilder strb = new StringBuilder("The Mediator Pattern!");
System.out.println(strb.toString());
MediatorPattern mp = new MediatorPattern();
mp.showMediatorPattern();
}//end main()
}//end class MediatorPattern
下面是UML图:
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
标签: