首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

Chain Of Responsibility(责任链方式)

2012-10-25 
Chain Of Responsibility(责任链模式)public abstract class Base{??//属性类型自定义?protected Chain fi

Chain Of Responsibility(责任链模式)

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

热点排行