夢(mèng)幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評(píng)論 :: 0 Trackbacks
          用一個(gè)中介對(duì)象來封裝一系統(tǒng)的對(duì)象交互。中介者使各對(duì)象不需要顯示地相互引用,從而使其耦合松散,而可以獨(dú)立改變他們之間的效死.
          組成部份:
          1.中介者
          2.具體中介者
          3.同事
          4.具體同事
          組成部份之間的關(guān)系:

          例子:
          1.人
          package simpleMediator;

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

          public abstract class Person
          {
              
          /** 姓名 */
              
          private String name;
              
              
          /** 條件 */
              
          private int condition;
              
              
          /** 中介者(不管男孩還是女孩,都需要知道中介者,通過中介者去找對(duì)象) */
              
          private Mediator mediator;
              
              
          /**
               * <ul>
               * <li>Description:[構(gòu)造方法]</li>
               * <ul>
               * 
               * 
          @param name
               * 
          @param condition
               * 
          @param mediator
               
          */

              
          public Person(String name, int condition, Mediator mediator)
              
          {
                  
          this.name = name;
                  
          this.condition = condition;
                  
          this.mediator = mediator;
              }

              
              
          /**
               * <ul>
               * <li>Description:[找對(duì)象]</li>
               * <li>Created by [Huyvanpull] [2011-8-3]</li>
               * <li>Midified by [modifier] [modified time]</li>
               * </ul>
               * 
               * 
          @param person
               * 
          @return
               
          */

              
          public abstract boolean findPartner(Person person);
              
              
          public String getName()
              
          {
                  
          return name;
              }

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

              
              
          public int getCondition()
              
          {
                  
          return condition;
              }

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

              
              
          public Mediator getMediator()
              
          {
                  
          return mediator;
              }

              
              
          public void setMediator(Mediator mediator)
              
          {
                  
          this.mediator = mediator;
              }

          }


          2.男孩
          package simpleMediator;

          public class Boy extends Person
          {   
              
          /**
               * <ul>
               * <li>Description:[構(gòu)造方法]</li>
               * <ul>
               * 
               * 
          @param name
               * 
          @param condition
               * 
          @param mediator
               
          */

              
          public Boy(String name, int condition, Mediator mediator)
              
          {
                  
          super(name, condition, mediator);
              }

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

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

              
          }


          3.女孩
          package simpleMediator;

          public class Girl extends Person
          {
              
              
          /**
               * <ul>
               * <li>Description:[構(gòu)造方法]</li>
               * <ul>
               * 
               * 
          @param name
               * 
          @param condition
               * 
          @param mediator
               
          */

              
          public Girl(String name, int condition, Mediator mediator)
              
          {
                  
          super(name, condition, mediator);
              }

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

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

              
          }


          4.婚介
          package simpleMediator;

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

          public interface Mediator
          {
              
              
          /**
               * <ul>
               * <li>Description:[男孩登記]</li>
               * <li>Created by [Huyvanpull] [2011-8-3]</li>
               * <li>Midified by [modifier] [modified time]</li>
               * </ul>
               * 
               * 
          @param boy
               
          */

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

              
          public void registGirl(Girl girl);
              
              
          /**
               * <ul>
               * <li>Description:[介紹對(duì)象]</li>
               * <li>Created by [Huyvanpull] [2011-8-3]</li>
               * <li>Midified by [modifier] [modified time]</li>
               * </ul>
               * 
               * 
          @param person
               * 
          @return
               
          */

              
          public boolean findPartner(Person person);
          }


          5.具體婚介
          package simpleMediator;

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

          public class RedSunMediator implements Mediator
          {
              
          /** 男孩 */
              
          private Boy boy;
              
              
          /** 女孩 */
              
          private Girl girl;
              
              
          /* (non-Javadoc)
               * @see simpleMediator.Mediator#registBoy(simpleMediator.Boy)
               
          */

              
          public void registBoy(Boy boy)
              
          {
                  
          this.boy = boy;
              }

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

              
          public void registGirl(Girl girl)
              
          {
                  
          this.girl = girl;
              }

              
              
          // 根據(jù)各自的條件匹配
              public boolean findPartner(Person person)
              
          {
                  
          if (person instanceof Boy)
                  
          {
                      
          this.boy = (Boy) person;
                  }

                  
          else
                  
          {
                      
          this.girl = (Girl) person;
                  }

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

          }


          6.測(cè)試
          package simpleMediator;

          public class Test
          {
              
          public static void main(String[] args)
              
          {
                  
          // 紅日婚介所開張
                  Mediator mediator = new RedSunMediator();
                  
                  
          // 一個(gè)名為小趙的男孩,條件為2500
                  Boy xiaoZhao = new Boy("小趙"2500, mediator);
                  mediator.registBoy(xiaoZhao);
                  
                  
          // 一個(gè)名為小錢的女孩,條件為為2500
                  Girl xiaoQian = new Girl("小錢"2500, mediator);
                  mediator.registGirl(xiaoQian);
                  
                  
          // 如果條件符合,則partner成立
                  xiaoZhao.findPartner(xiaoQian);
                  
                  
          // 一個(gè)名為lanlan的女孩,條件為90
                  Girl xiaosun = new Girl("小孫"5000, mediator);
                  mediator.registGirl(xiaosun);
                  xiaosun.findPartner(xiaoZhao);
              }

          }

          總結(jié)
               中介者模式很容易在系統(tǒng)中應(yīng)用,也很容易在系統(tǒng)中誤用。當(dāng)系統(tǒng)出現(xiàn)了“多對(duì)多”交互復(fù)雜的對(duì)象群,不要急于使用中介者模式,而要先反思你的系統(tǒng)在設(shè)計(jì)上是不是合理。
          posted on 2011-08-03 17:04 HUIKK 閱讀(288) 評(píng)論(0)  編輯  收藏 所屬分類: Design Patterns
          主站蜘蛛池模板: 延庆县| 贡嘎县| 东明县| 临安市| 新昌县| 昭平县| 莲花县| 张北县| 永胜县| 濉溪县| 嘉禾县| 唐海县| 获嘉县| 镇远县| 富川| 石景山区| 高安市| 黑山县| 万安县| 宿松县| 沭阳县| 新巴尔虎左旗| 博湖县| 理塘县| 灵璧县| 东乌| 垣曲县| 石景山区| 荔波县| 长丰县| 温泉县| 吉水县| 米泉市| 定陶县| 禄丰县| 大埔区| 昆山市| 保靖县| 泽库县| 洞头县| 齐齐哈尔市|