我的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 閱讀(473) 評論(0)  編輯  收藏 所屬分類: java

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

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 儋州市| 拉孜县| 盐池县| 五寨县| 阳新县| 黑水县| 曲水县| 通州区| 涞水县| 九江县| 蒲江县| 称多县| 和田市| 长泰县| 探索| 乐都县| 宁强县| 洛阳市| 松滋市| 滁州市| 竹溪县| 固安县| 杭州市| 宁河县| 隆安县| 寿阳县| 九龙城区| 铜鼓县| 凤阳县| 清河县| 德兴市| 阿尔山市| 凤翔县| 高邮市| 江华| 望都县| 交口县| 尖扎县| 武安市| 米易县| 仁寿县|