我的java天地

          另一種責(zé)任鏈實(shí)現(xiàn)模式

          package com.toby.zerenliang;

          public interface IHandle {
              /**
               * 邏輯處理
               * 
               * 
          @param mtMsg    MT數(shù)據(jù)    
               * 
          @return         成功返回true,否則false
               
          */
              public boolean process(MtMsg mtMsg);
              
              /**
               * 增加下級Handler.寄主Handler可以看情況調(diào)用該下級Handler
               * 
               * 
          @param hdl    下級Handler
               * 
          @return        下級Handler
               
          */
              public IHandle addNextHandler(IHandle hdl);
          }


          package com.toby.zerenliang;

          public class MtMsg {
              public int age;
              public String name;
          }



          package com.toby.zerenliang;

          public class OneHandler implements IHandle {
              private IHandle nextHdl;
              
              @Override
              public IHandle addNextHandler(IHandle hdl) {
                  this.nextHdl = hdl;
                  return this.nextHdl;
              }

              @Override
              public boolean process(MtMsg mtMsg) {
                  /**
                   * 業(yè)務(wù)邏輯處理
                   
          */
                  System.out.println("業(yè)務(wù)邏輯處理one");
                  if(nextHdl != null)
                      return nextHdl.process(mtMsg);
                  else
                      return true;
              }

          }





          package com.toby.zerenliang;

          public class TwoHandler implements IHandle {
              private IHandle nextHdl;
              @Override
              public IHandle addNextHandler(IHandle hdl) {
                  this.nextHdl = hdl;
                  return this.nextHdl;
              }

              @Override
              public boolean process(MtMsg mtMsg) {
                  /**
                   * 業(yè)務(wù)邏輯處理
                   
          */
                  System.out.println("業(yè)務(wù)邏輯處理two");
                  if(nextHdl != null)
                      return nextHdl.process(mtMsg);
                  else
                      return true;
              }

          }




          package com.toby.zerenliang;

          public class Test {

              /**
               * 
          @param args
               
          */
              public static void main(String[] args) {
                  MtMsg mm = new MtMsg();
                  mm.age = 17;
                  mm.name = "名字";
                  IHandle iHandle = new OneHandler();
                  iHandle.addNextHandler(new TwoHandler());
                  
                  iHandle.process(mm);
              }

          }

          posted on 2012-03-15 11:18 tobyxiong 閱讀(473) 評論(0)  編輯  收藏 所屬分類: java

          <2012年3月>
          26272829123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 和田市| 镇沅| 岳西县| 凌海市| 弥渡县| 昌都县| 洪雅县| 满城县| 高雄市| 尉犁县| 武鸣县| 凤翔县| 临西县| 绥芬河市| 谷城县| 花垣县| 江口县| 赤水市| 温宿县| 龙陵县| 台前县| 漠河县| 建湖县| 孟村| 绵阳市| 瓦房店市| 罗平县| 大洼县| 溆浦县| 阿合奇县| 定安县| 无极县| 招远市| 永德县| 乌鲁木齐市| 博白县| 永靖县| 游戏| 肥西县| 淮南市| 宜章县|