我的java天地

          另一種責任鏈實現模式

          package com.toby.zerenliang;

          public interface IHandle {
              /**
               * 邏輯處理
               * 
               * 
          @param mtMsg    MT數據    
               * 
          @return         成功返回true,否則false
               
          */
              public boolean process(MtMsg mtMsg);
              
              /**
               * 增加下級Handler.寄主Handler可以看情況調用該下級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) {
                  /**
                   * 業務邏輯處理
                   
          */
                  System.out.println("業務邏輯處理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) {
                  /**
                   * 業務邏輯處理
                   
          */
                  System.out.println("業務邏輯處理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 閱讀(465) 評論(0)  編輯  收藏 所屬分類: java

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

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 花莲市| 蒙自县| 赫章县| 剑阁县| 甘南县| 洛阳市| 六枝特区| 喀喇| 泾川县| 湘潭市| 杭州市| 临潭县| 泸溪县| 宁武县| 察雅县| 盖州市| 延川县| 诸暨市| 咸丰县| 峨边| 华池县| 和林格尔县| 延川县| 南投市| 攀枝花市| 丰原市| 东丽区| 四子王旗| 肇庆市| 醴陵市| 日喀则市| 尼木县| 临夏县| 江达县| 南雄市| 百色市| 湖北省| 闵行区| 印江| 慈利县| 绥棱县|