Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4205590
  • 博文数量: 176
  • 博客积分: 10059
  • 博客等级: 上将
  • 技术积分: 4681
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-24 12:27
文章分类

全部博文(176)

文章存档

2012年(1)

2011年(4)

2010年(14)

2009年(71)

2008年(103)

分类: 项目管理

2008-06-24 11:04:06

Mediator模式也叫中介者模式,是由GoF提出的23种软件设计模式的一种。Mediator模式是行为模式之一,Mediator模式定义一个“中介”对象来封装对象的交互行为。

本文介绍设计模式中的(Mediator)模式的概念,用法,以及实际应用中怎么样使用Mediator模式进行开发。

Mediator模式的概念
Mediator模式是行为模式之一,Mediator模式定义一个对象来封装对象的交互行为。从而降低对象间的耦合程度。
在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互,Mediator对象起着控制器的作用。

Mediator模式的应用场景与利点
当多个类之间相互依存,互相调用,关系过于复杂时,可以考虑使用Mediator模式来简化它们之间的关系。

使用Mediator模式时的优点
1,将系统按功能分割成更小的对象,符合类的最小设计原则
2,对关联对象的集中控制
3,减小类的耦合程度,明确类之间的相互关系:当类之间的关系过于复杂时,其中任何一个类的修改都会影响到其他类,不符合 ,而Mediator模式将原来相互依存的多对多的类之间的关系简化为Mediator控制类与其他关联类的一对多的关系,当其中一个类修改时,可以对其他关联类不产生影响(即使有修改,也集中在Mediator控制类)。
4,有利于提高类的重用性


Mediator模式的应用范例
下面,我们使用Mediator模式模拟男孩女孩找对象的行为:在Boy类中,需要调用Girl类的方法;相反,在Girl类中,也需要调用Boy类的方法。所以我们采用Mediator模式来实现这个过程。

文件一览:
Client
    测试类
Mediator
    Mediator中介者类,这里省略了抽象类
Person
    关联类(Boy, Girl)的抽象类。关联类之间并不一定需要共享一个父类,这里为了方便,为他们抽象出一个Person类
Boy
    关联类之Boy,跟Girl类相关联
Girl
    关联类之Girl,跟Boy类相关联

代码:
public class Client {

    /**
     * Test Mediator Pattern
     */

    public static void main(String[] args) {
        //婚姻中介所开张了
        Mediator mediator = new Mediator();
        
        //一个名为Zhangshan的男孩,条件为80
        Boy zhangshan = new Boy(mediator, "Zhangshan", 80);
        Girl lili = new Girl(mediator, "Lili", 80);
        
        //如果条件符合,则partner成立
        zhangshan.findPartner(lili);

        //一个名为lanlan的女孩,条件为90
        Girl lanlan = new Girl(mediator, "lanlan", 90);
        lanlan.findPartner(zhangshan);
    }

}


/**
* 关联类(Boy, Girl)的抽象类
* 关联类之间并不一定需要共享一个父类,这里为了方便,为他们抽象出一个Person类
*/

abstract class Person {
    //姓名
    String name;
    //条件
    int condition;
    //中介者(不管男孩还是女孩,都需要知道中介者,通过中介者去找对象)
    Mediator mediator;
    
    public Person(Mediator mediator, String name, int condition) {
        this.mediator = mediator;
        this.name = name;
        this.condition = condition;
    }
    
    public abstract boolean findPartner(Person person);

    
    public int getCondition() {
        return condition;
    }

    public void setCondition(int condition) {
        this.condition = condition;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

/**
* 关联类之Boy,跟Girl类相关联
* 男孩
*
*/

class Boy extends Person {

    public Boy(Mediator mediator, String name, int condition) {
        super(mediator, name, condition);
        //注册自己
        mediator.registBoy(this);
    }

    @Override
    public boolean findPartner(Person person) {
        System.out.println("Boy try to find girl.");
        return mediator.findPartner(person);
    }
}

/**
* 关联类之Girl,跟Boy类相关联
* 女孩
*
*/

class Girl extends Person {
    public Girl(Mediator mediator, String name, int condition) {
        super(mediator, name, condition);
        //注册自己
        mediator.registGirl(this);
    }

    @Override
    public boolean findPartner(Person person) {
        System.out.println("Girl try to find boy.");
        return mediator.findPartner(person);
    }
}

/**
* Mediator
* 婚姻中介者
*
*/

class Mediator {
    Boy boy;
    Girl girl;
    
    //男孩注册
    public void registBoy(Boy boy) {
        this.boy = boy;
    }
    
    //女孩注册
    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 ret = this.boy.getCondition() == this.girl.getCondition();
        
        System.out.println(this.boy.getName() + " & " + this.girl.getName() + " is partner: " + ret);
        
        return ret;
    }
}



执行Client,输出结果:
C:\Mediator>javac *.java
C:\Mediator>java Client
Boy try to find girl.
Zhangshan & Lili is partner: true
Girl try to find boy.
Zhangshan & lanlan is partner: false
C:\Mediator>

阅读(3769) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-04-09 00:51:37

通俗易懂 谢谢!