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

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

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(3)

          隨筆分類(144)

          隨筆檔案(157)

          相冊

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 内丘县| 普陀区| 平遥县| 鱼台县| 南陵县| 长沙市| 昌宁县| 佛冈县| 万安县| 北票市| 新巴尔虎左旗| 六安市| 开原市| 通辽市| 汉源县| 苍南县| 白银市| 商城县| 佳木斯市| 乌什县| 淅川县| 邻水| 文化| 固阳县| 分宜县| 上林县| 海南省| 盐亭县| 赫章县| 丁青县| 铜川市| 左贡县| 蓬安县| 常德市| 霞浦县| 土默特左旗| 江华| 黄平县| 旌德县| 景东| 尼木县|