Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1277295
  • 博文数量: 185
  • 博客积分: 50
  • 博客等级: 民兵
  • 技术积分: 3934
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-11 13:11
个人简介

iihero@ChinaUnix, ehero.[iihero] 数据库技术的痴迷爱好者. 您可以通过iihero AT qq.com联系到我 以下是我的三本图书: Sybase ASE in Action, Oracle Spatial及OCI高级编程, Java2网络协议内幕

文章分类

全部博文(185)

文章存档

2014年(4)

2013年(181)

分类: 项目管理

2013-07-25 10:49:53

概述

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

适用性

    1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。

    2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象。

    3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。
			

参与者

    1.Mediator
      中介者定义一个接口用于与各同事(Colleague)对象通信。

    2.ConcreteMediator
      具体中介者通过协调各同事对象实现协作行为。
      了解并维护它的各个同事。

    3.Colleagueclass
      每一个同事类都知道它的中介者对象。
      每一个同事对象在需与其他的同事通信的时候,与它的中介者通信


类图

示例


  1. package com.sql9.actioned;  
  2.   
  3. /** 
  4.  * 中介者模式 示例, 以二手房买卖为例 :) 
  5.  * @author sean 
  6.  */  
  7.   
  8. abstract class Mediator {  
  9.     public abstract void notice(String content);  
  10. }  
  11.   
  12. interface Customer {  
  13.     void action();  
  14. }  
  15.   
  16. // 买方  
  17. class Buyer implements Customer {  
  18.       
  19.     String target;  
  20.       
  21.     public Buyer(String target) {  
  22.         this.target = target;  
  23.     }  
  24.       
  25.     @Override  
  26.     public void action() {  
  27.         System.out.println(target + " will buy the target house");  
  28.     }  
  29.       
  30. }  
  31.   
  32. // 卖方  
  33. class Seller implements Customer {  
  34.     String source;  
  35.     String address;  
  36.     public Seller(String source, String address) {  
  37.         this.source = source;  
  38.         this.address = address;  
  39.     }  
  40.     @Override  
  41.     public void action() {  
  42.         System.out.println(source + " will sell the house: " + address);  
  43.     }  
  44.       
  45. }  
  46.   
  47. class ConcreteMediator extends Mediator {  
  48.   
  49.     private Customer custA; //买方  
  50.     private Customer custB; //卖方  
  51.       
  52.     public ConcreteMediator(Customer custA, Customer custB) {  
  53.         this.custA = custA;  
  54.         this.custB = custB;  
  55.     }  
  56.       
  57.     @Override  
  58.     public void notice(String content) {  
  59.         System.out.println("notice conent: " + content);  
  60.         if ("buyer".equals(content)) {  
  61.             custA.action();  
  62.         }  
  63.         else if ("seller".equals(content)) {  
  64.             custB.action();  
  65.         }  
  66.         else {  
  67.             System.out.println("invalid notice content");  
  68.         }  
  69.     }  
  70.       
  71. }  
  72.   
  73. public class MediatorTest {  
  74.   
  75.     public static void main(String[] args) {  
  76.         Mediator mediator = new ConcreteMediator(new Buyer("张三"), new Seller("李四""abc广场小区x号院yyy"));  
  77.         mediator.notice("buyer");  
  78.         mediator.notice("seller");  
  79.     }  
  80.   
  81. }  


结果


  1. notice conent: buyer  
  2. 张三 will buy the target house  
  3. notice conent: seller  
  4. 李四 will sell the house: abc广场小区x号院yyy  

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