zoukankan      html  css  js  c++  java
  • 设计模式(九)——中介者模式

    1.描述

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

    2.模式的使用

    ·中介者(Mediator):中介者是一个接口,该接口定义了用于同事(Colleague)对象之间进行通信的方法。

    ·具体中介者(ConcreteMediator):具体中介者是实现中介者接口的类。具体中介者需要包含有所有具体同事(ConcreteColleague)的引用,并通过实现中介者接口中的方法来满足具体同事之间的通信请求。

    ·同事(Colleague):一个接口,规定了具体同事需要实现的方法。

    ·具体同事(ConcreteColleague):实现同事接口的类。具体同事需要包含具体中介者的引用,一个具体同事需要和其他具体同事交互时,只需将自己的请求发送给中介者即可。

    3.使用情景

    ·许多对象以复杂的形式交互,所导致的依赖关系使系统难以理解和维护。

    ·一个对象引用其他很多对象,导致该对象难以被复用。

    ·多各对象需要交互,不允许互相引用。

    4.优点

    ·避免了许多需要通信的对象之间的显示引用。

    ·通过中介者将分散于多个对象之间的交互行为集中起来。当交互行为需要改变时,只需改动中介者,不用改变每一个同事。

    ·各个同事完全解耦

    5.UML图

    6.案例

    现在有多个卖家(Seller)发布商品信息关注购买信息,多个买家(Buyer)发布购买信息关注出售信息。要求买家发布的信息卖家都能接受,卖家发布的信息买家都能接受。

      1 package mediator;
      2 
      3 import java.util.HashMap;
      4 import java.util.Iterator;
      5 import java.util.Set;
      6 
      7 public class test1 {
      8 
      9     public static void main(String[] args) {
     10         ConcreteMediator mediator  = new ConcreteMediator();
     11         Buyer b1 = new Buyer1(mediator);
     12         b1.setName("买家1");
     13         Buyer1 b2 = new Buyer1(mediator);
     14         b2.setName("买家2");
     15         Seller s1 = new Seller1(mediator);
     16         s1.setName("卖家1");
     17         Seller s2 = new Seller1(mediator);
     18         s2.setName("卖家2");
     19         b1.sendMess("我要买房子");
     20         b2.sendMess("我要买汽车");
     21         s1.sendMess("我要卖股票");
     22         s2.sendMess("我要卖房子");
     23     }
     24 
     25 }
     26 
     27 /*
     28  * 同事
     29  */
     30 interface Colleague{
     31     public void sendMess(String mess); //发布信息
     32     public void receiveMess(Colleague colleague, String mess);  //接受信息
     33     public void setName(String mess);
     34     public String getName();
     35 }
     36 
     37 /*
     38  * 买家抽象类
     39  */
     40 abstract class Buyer implements Colleague{
     41     private final static String IDENTITY= "我是买家";
     42     public String getIdentity(){
     43         return IDENTITY;
     44     }
     45 }
     46 
     47 /*
     48  * 卖家抽象类
     49  */
     50 abstract class Seller implements Colleague{
     51     private final static String IDENTITY= "我是卖家";
     52     public String getIdentity(){
     53         return IDENTITY;
     54     }
     55 }
     56 /*
     57  * 中介者
     58  */
     59 interface Mediator{
     60     public void registColleague(Colleague colleague);
     61     public void deliverMess(Colleague colleague, String mess);
     62 }
     63 
     64 /*
     65  * 具体中介者
     66  */
     67 class ConcreteMediator implements Mediator{
     68     /*
     69      * 在这里采用HashMap容器。因为买卖双方需要进行一对一的信息交互才能达成一致,肯定要对数
     70      * 据进行查询,这种结构查询很快。但在本案例中,只实现了一对多的广播信息功能,实
     71      * 际上没用到HashMap的查询优势。如果只进行遍历,可以换成List结构。
     72      */
     73     private HashMap<Integer, Buyer> buyerMap; //储存买家对象
     74     private HashMap<Integer, Seller> sellerMap;  //储存卖家对象
     75     ConcreteMediator(){
     76         this.buyerMap = new HashMap<Integer, Buyer>();
     77         this.sellerMap = new HashMap<Integer, Seller>();
     78     }
     79     public void registColleague(Colleague colleague) {
     80         //先判断是哪一类,再储存买家、卖家的引用
     81         if(colleague.getClass().getGenericSuperclass().toString().equals("class mediator.Buyer"))
     82             this.buyerMap.put(colleague.hashCode(), (Buyer) colleague);
     83         if(colleague.getClass().getGenericSuperclass().toString().equals("class mediator.Seller"))
     84             this.sellerMap.put(colleague.hashCode(), (Seller) colleague);
     85     }
     86 
     87     public void deliverMess(Colleague colleague, String mess) {
     88         //买家将消息发送给所有卖家
     89         if(colleague.getClass().getGenericSuperclass().toString().equals("class mediator.Buyer")){
     90             //遍历HashMap的方法
     91             Set<Integer> set = this.sellerMap.keySet();
     92             Iterator<Integer> iterator = set.iterator();
     93             Integer i;
     94             while(iterator.hasNext()){
     95                 i = iterator.next();
     96                 this.sellerMap.get(i).receiveMess(this.sellerMap.get(i),mess);
     97             }
     98         }
     99         //卖家将所有消息发送给买家
    100         if(colleague.getClass().getGenericSuperclass().toString().equals("class mediator.Seller")){
    101             //遍历HashMap的方法
    102             Set<Integer> set = this.buyerMap.keySet();
    103             Iterator<Integer> iterator = set.iterator();
    104             Integer i;
    105             while(iterator.hasNext()){
    106                 i = iterator.next();
    107                 this.buyerMap.get(i).receiveMess(this.buyerMap.get(i), mess);
    108             }
    109         }
    110     }
    111     
    112 } 
    113 
    114 /*
    115  * 具体同事
    116  */
    117 class Buyer1 extends Buyer{
    118     private String name;
    119     Mediator mediator; //存储中介者的引用,也许还有其他中介者用来实现其他的信息交互。
    120     Buyer1(Mediator mediator){
    121         this.mediator  = mediator;
    122         mediator.registColleague(this);
    123     }
    124     public void sendMess(String mess) {
    125         mediator.deliverMess(this, mess);
    126     }
    127 
    128     public void receiveMess(Colleague colleague, String mess) {
    129         System.out.println("卖家发出消息   " + colleague.getName() + "接受信息:" + mess);
    130     }
    131 
    132     public void setName(String name) {
    133         this.name  = name;
    134     }
    135 
    136     public String getName() {
    137         return name;
    138     }
    139 }
    140 
    141 /*
    142  * 具体同事
    143  */
    144 class Seller1 extends Seller{
    145     private String name;
    146     Mediator mediator;
    147     Seller1(Mediator mediator){
    148         this.mediator  = mediator;
    149         mediator.registColleague(this);
    150     }
    151     public void sendMess(String mess) {
    152         mediator.deliverMess(this, mess);
    153     }
    154 
    155     public void receiveMess(Colleague colleague, String mess) {
    156         System.out.println("买家发出消息   " + colleague.getName() + "接受信息:" + mess);
    157     }
    158 
    159     public void setName(String name) {
    160         this.name  = name;
    161     }
    162 
    163     public String getName() {
    164         return name;
    165     }
    166 }

  • 相关阅读:
    实现页面切换(动画效果实现,不用ViewPager)
    “仅仅生一个娃”--设计模式中的单例模式
    ZOJ
    【J2SE高速进阶】——多线程之synchronized
    [LeetCode] Search a 2D Matrix II
    leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
    [CentOS]怎样解决gcc版本号冲突?
    从0开始学习 GITHUB 系列之「GITHUB 常见的几种操作」【转】
    从0开始学习 GITHUB 系列之「向GITHUB 提交代码」【转】
    从0开始学习 GITHUB 系列之「GIT 速成」【转】
  • 原文地址:https://www.cnblogs.com/cxy2016/p/7592598.html
Copyright © 2011-2022 走看看