用一个中介对象来封装一系统的对象交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而可以独立改变他们之间的效死.
组成部份:
1.中介者
2.具体中介者
3.同事
4.具体同事
组成部份之间的关系:

例子:
1.人
 package simpleMediator;
package simpleMediator;


 /** *//**
/** *//**
 * <ul>
 * <ul>
 * <li>Title:[Person]</li>
 * <li>Title:[Person]</li>
 * <li>Description: [人(抽象同事)]</li>
 * <li>Description: [人(抽象同事)]</li>
 * <li>Copyright 2009 Upengs Co., Ltd.</li>
 * <li>Copyright 2009 Upengs Co., Ltd.</li>
 * <li>All right reserved.</li>
 * <li>All right reserved.</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * </ul>
 * </ul>
 *
 * 
 * @version 1.0
 * @version 1.0
 */
 */
 public abstract class Person
public abstract class Person


 {
{

 /** *//** 姓名 */
    /** *//** 姓名 */
 private String name;
    private String name;
 
    

 /** *//** 条件 */
    /** *//** 条件 */
 private int condition;
    private int condition;
 
    

 /** *//** 中介者(不管男孩还是女孩,都需要知道中介者,通过中介者去找对象) */
    /** *//** 中介者(不管男孩还是女孩,都需要知道中介者,通过中介者去找对象) */
 private Mediator mediator;
    private Mediator mediator;
 
    

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[构造方法]</li>
     * <li>Description:[构造方法]</li>
 * <ul>
     * <ul>
 *
     * 
 * @param name
     * @param name
 * @param condition
     * @param condition
 * @param mediator
     * @param mediator
 */
     */
 public Person(String name, int condition, Mediator mediator)
    public Person(String name, int condition, Mediator mediator)

 
     {
{
 this.name = name;
        this.name = name;
 this.condition = condition;
        this.condition = condition;
 this.mediator = mediator;
        this.mediator = mediator;
 }
    }
 
    

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[找对象]</li>
     * <li>Description:[找对象]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
     * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
     * <li>Midified by [modifier] [modified time]</li>
 * </ul>
     * </ul>
 *
     * 
 * @param person
     * @param person
 * @return
     * @return
 */
     */
 public abstract boolean findPartner(Person person);
    public abstract boolean findPartner(Person person);
 
    
 public String getName()
    public String getName()

 
     {
{
 return name;
        return name;
 }
    }
 
    
 public void setName(String name)
    public void setName(String name)

 
     {
{
 this.name = name;
        this.name = name;
 }
    }
 
    
 public int getCondition()
    public int getCondition()

 
     {
{
 return condition;
        return condition;
 }
    }
 
    
 public void setCondition(int condition)
    public void setCondition(int condition)

 
     {
{
 this.condition = condition;
        this.condition = condition;
 }
    }
 
    
 public Mediator getMediator()
    public Mediator getMediator()

 
     {
{
 return mediator;
        return mediator;
 }
    }
 
    
 public void setMediator(Mediator mediator)
    public void setMediator(Mediator mediator)

 
     {
{
 this.mediator = mediator;
        this.mediator = mediator;
 }
    }
 }
}

2.男孩
 package simpleMediator;
package simpleMediator;

 public class Boy extends Person
public class Boy extends Person


 {
{   

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[构造方法]</li>
     * <li>Description:[构造方法]</li>
 * <ul>
     * <ul>
 *
     * 
 * @param name
     * @param name
 * @param condition
     * @param condition
 * @param mediator
     * @param mediator
 */
     */
 public Boy(String name, int condition, Mediator mediator)
    public Boy(String name, int condition, Mediator mediator)

 
     {
{
 super(name, condition, mediator);
        super(name, condition, mediator);
 }
    }
 
    

 /**//*
    /**//*
 * (non-Javadoc)
     * (non-Javadoc)
 *
     * 
 * @see simpleMediator.Person#findPartner(simpleMediator.Person)
     * @see simpleMediator.Person#findPartner(simpleMediator.Person)
 */
     */
 public boolean findPartner(Person person)
    public boolean findPartner(Person person)

 
     {
{
 System.out.println("boy try to find girl.");
        System.out.println("boy try to find girl.");
 this.getMediator().findPartner(person);
        this.getMediator().findPartner(person);
 return false;
        return false;
 }
    }
 
    
 }
}

3.女孩
 package simpleMediator;
package simpleMediator;

 public class Girl extends Person
public class Girl extends Person


 {
{
 
    

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[构造方法]</li>
     * <li>Description:[构造方法]</li>
 * <ul>
     * <ul>
 *
     * 
 * @param name
     * @param name
 * @param condition
     * @param condition
 * @param mediator
     * @param mediator
 */
     */
 public Girl(String name, int condition, Mediator mediator)
    public Girl(String name, int condition, Mediator mediator)

 
     {
{
 super(name, condition, mediator);
        super(name, condition, mediator);
 }
    }
 
    

 /**//*
    /**//*
 * (non-Javadoc)
     * (non-Javadoc)
 *
     * 
 * @see simpleMediator.Person#findPartner(simpleMediator.Person)
     * @see simpleMediator.Person#findPartner(simpleMediator.Person)
 */
     */
 public boolean findPartner(Person person)
    public boolean findPartner(Person person)

 
     {
{
 System.out.println("girl try to find boy.");
        System.out.println("girl try to find boy.");
 this.getMediator().findPartner(person);
        this.getMediator().findPartner(person);
 return false;
        return false;
 }
    }
 
    
 }
}

4.婚介
 package simpleMediator;
package simpleMediator;


 /** *//**
/** *//**
 * <ul>
 * <ul>
 * <li>Title:[Mediator]</li>
 * <li>Title:[Mediator]</li>
 * <li>Description: [婚姻登记]</li>
 * <li>Description: [婚姻登记]</li>
 * <li>Copyright 2009 Upengs Co., Ltd.</li>
 * <li>Copyright 2009 Upengs Co., Ltd.</li>
 * <li>All right reserved.</li>
 * <li>All right reserved.</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * </ul>
 * </ul>
 *
 * 
 * @version 1.0
 * @version 1.0
 */
 */
 public interface Mediator
public interface Mediator


 {
{
 
    

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[男孩登记]</li>
     * <li>Description:[男孩登记]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
     * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
     * <li>Midified by [modifier] [modified time]</li>
 * </ul>
     * </ul>
 *
     * 
 * @param boy
     * @param boy
 */
     */
 public void registBoy(Boy boy);
    public void registBoy(Boy boy);
 
    

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[女孩登记]</li>
     * <li>Description:[女孩登记]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
     * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
     * <li>Midified by [modifier] [modified time]</li>
 * </ul>
     * </ul>
 *
     * 
 * @param girl
     * @param girl
 */
     */
 public void registGirl(Girl girl);
    public void registGirl(Girl girl);
 
    

 /** *//**
    /** *//**
 * <ul>
     * <ul>
 * <li>Description:[介绍对象]</li>
     * <li>Description:[介绍对象]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
     * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
     * <li>Midified by [modifier] [modified time]</li>
 * </ul>
     * </ul>
 *
     * 
 * @param person
     * @param person
 * @return
     * @return
 */
     */
 public boolean findPartner(Person person);
    public boolean findPartner(Person person);
 }
}5.具体婚介
 package simpleMediator;
package simpleMediator;


 /** *//**
/** *//**
 * <ul>
 * <ul>
 * <li>Title:[RedSunMediator]</li>
 * <li>Title:[RedSunMediator]</li>
 * <li>Description: [红日婚介所]</li>
 * <li>Description: [红日婚介所]</li>
 * <li>Copyright 2009 Upengs Co., Ltd.</li>
 * <li>Copyright 2009 Upengs Co., Ltd.</li>
 * <li>All right reserved.</li>
 * <li>All right reserved.</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Created by [Huyvanpull] [2011-8-3]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * <li>Midified by [modifier] [modified time]</li>
 * </ul>
 * </ul>
 *
 * 
 * @version 1.0
 * @version 1.0
 */
 */
 public class RedSunMediator implements Mediator
public class RedSunMediator implements Mediator


 {
{

 /** *//** 男孩 */
    /** *//** 男孩 */
 private Boy boy;
    private Boy boy;
 
    

 /** *//** 女孩 */
    /** *//** 女孩 */
 private Girl girl;
    private Girl girl;
 
    

 /**//* (non-Javadoc)
    /**//* (non-Javadoc)
 * @see simpleMediator.Mediator#registBoy(simpleMediator.Boy)
     * @see simpleMediator.Mediator#registBoy(simpleMediator.Boy)
 */
     */
 public void registBoy(Boy boy)
    public void registBoy(Boy boy)

 
     {
{
 this.boy = boy;
        this.boy = boy;
 }
    }
 
    

 /**//* (non-Javadoc)
    /**//* (non-Javadoc)
 * @see simpleMediator.Mediator#registGirl(simpleMediator.Girl)
     * @see simpleMediator.Mediator#registGirl(simpleMediator.Girl)
 */
     */
 public void registGirl(Girl girl)
    public void registGirl(Girl girl)

 
     {
{
 this.girl = girl;
        this.girl = girl;
 }
    }
 
    
 // 根据各自的条件匹配
    // 根据各自的条件匹配
 public boolean findPartner(Person person)
    public boolean findPartner(Person person)

 
     {
{
 if (person instanceof Boy)
        if (person instanceof Boy)

 
         {
{
 this.boy = (Boy) person;
            this.boy = (Boy) person;
 }
        }
 else
        else

 
         {
{
 this.girl = (Girl) person;
            this.girl = (Girl) person;
 }
        }
 
        
 boolean success = this.boy.getCondition() == this.girl.getCondition();
        boolean success = this.boy.getCondition() == this.girl.getCondition();
 
        
 StringBuffer info = new StringBuffer();
        StringBuffer info = new StringBuffer();
 info.append(this.boy.getName());
        info.append(this.boy.getName());
 info.append(" vs ");
        info.append(" vs ");
 info.append(this.girl.getName());
        info.append(this.girl.getName());
 info.append(" is partner: " );
        info.append(" is partner: " );
 info.append(success);
        info.append(success);
 
        
 System.out.println(info);
        System.out.println(info);
 
        
 return success;
        return success;
 }
    }
 }
}

6.测试
 package simpleMediator;
package simpleMediator;

 public class Test
public class Test


 {
{
 public static void main(String[] args)
    public static void main(String[] args)

 
     {
{
 // 红日婚介所开张
        // 红日婚介所开张
 Mediator mediator = new RedSunMediator();
        Mediator mediator = new RedSunMediator();
 
        
 // 一个名为小赵的男孩,条件为2500
        // 一个名为小赵的男孩,条件为2500
 Boy xiaoZhao = new Boy("小赵", 2500, mediator);
        Boy xiaoZhao = new Boy("小赵", 2500, mediator);
 mediator.registBoy(xiaoZhao);
        mediator.registBoy(xiaoZhao);
 
        
 // 一个名为小钱的女孩,条件为为2500
        // 一个名为小钱的女孩,条件为为2500
 Girl xiaoQian = new Girl("小钱", 2500, mediator);
        Girl xiaoQian = new Girl("小钱", 2500, mediator);
 mediator.registGirl(xiaoQian);
        mediator.registGirl(xiaoQian);
 
        
 // 如果条件符合,则partner成立
        // 如果条件符合,则partner成立
 xiaoZhao.findPartner(xiaoQian);
        xiaoZhao.findPartner(xiaoQian);
 
        
 // 一个名为lanlan的女孩,条件为90
        // 一个名为lanlan的女孩,条件为90
 Girl xiaosun = new Girl("小孙", 5000, mediator);
        Girl xiaosun = new Girl("小孙", 5000, mediator);
 mediator.registGirl(xiaosun);
        mediator.registGirl(xiaosun);
 xiaosun.findPartner(xiaoZhao);
        xiaosun.findPartner(xiaoZhao);
 }
    }
 }
}

总结
     中介者模式很容易在系统中应用,也很容易在系统中误用。当系统出现了“多对多”交互复杂的对象群,不要急于使用中介者模式,而要先反思你的系统在设计上是不是合理。