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 类似写法