六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

Chain Of Responsibility(责任链模式)

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-26 13:35:14 | 显示全部楼层 |阅读模式
public abstract class Base{
  //属性类型自定义
 protected Chain firChain;
 /**
  * chain操作
  */
 public abstract Object submit(Object obj);
}
 
public class BaseImpl extends Base{
 public BaseImpl () {
  super();
  // 初始化职责链
  firChain = new AChain();
  BChain bChain = new BChain();
  CChain cChain = new CChain();
  firChain.setNextChain(bChain );
  bChain .setNextChain(cChain );
 }
 public Result submit(Object obj) {
  Result result = new Result();
  firChain.sendToChain(obj, result );
  return result ;
 }
}
 
public abstract class Chain {
 protected Chain nextChain;// 下一个执行节点
 /**
  * 得到此节点的下一个执行节点
  *
  * @return
  */
 public Chain getNextChain() {
  return nextChain;
 }
 /**
  * 添加此节点的下一个执行节点
  *
  * @param c
  */
 public void setNextChain(Chain nextChain) {
  this.nextChain = nextChain;
 }
 /**
  * 执行内容
  */
 public abstract void sendToChain(Object obj,
   Result result);
}
 
 
public class AChain extends Chain {
 @Override
 public void sendToChain(Object obj, Result result) {
    //具体操作
 
   //最后
   if (nextChain != null)
    nextChain.sendToChain(obj, result);
 }
}
BChain CChain 类似写法
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表