我的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)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大名县| 合江县| 长武县| 当阳市| 瓮安县| 清流县| 雅江县| 开封县| 山阳县| 朝阳区| 泰兴市| 韶关市| 定日县| 米泉市| 武邑县| 苗栗县| 乐亭县| 抚远县| 芮城县| 湘西| 安岳县| 郓城县| 新化县| 即墨市| 枞阳县| 阳城县| 玉溪市| 顺昌县| 囊谦县| 淄博市| 贡嘎县| 宁化县| 永胜县| 乌海市| 治县。| 武定县| 河北区| 平原县| 通城县| 华坪县| 织金县|