我的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 阅读(445) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(3)

随笔分类(144)

随笔档案(157)

相册

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜