美文网首页
责任链模式

责任链模式

作者: 北风第一支 | 来源:发表于2017-06-23 17:04 被阅读0次

当一个web请求到达的时候,去执行逻辑1,得到结果1;再根据结果1,执行逻辑2,得到结果2,……依次执行,最后得到结果N,也就是最终结果。

而具体到这个软件产品中,将具体要执行的逻辑1,逻辑2,……逻辑n视为command,用一个命令链给它们穿起来。但是还需要提供每一个command执行之后的上下文context。而上述需求,适用org.apache.commons.chain,就能够完全实现。

public abstract  class EntryChain {

    protected EntryChain nextChain;

    public abstract void handleEntryItem(EntryItem item, EntryLog entryLog);

    public void setNextChain(EntryChain nextChain) {
        this.nextChain = nextChain;
    }
}
public class BaseChain extends EntryChain {

    private final Logger logger = LoggerFactory.getLogger(BaseChain.class);
    @Override
    public void handleEntryItem(EntryItem entryItem, EntryLog entryLog) {
        Integer entryState = entryItem.getEntryState();
        if(entryStateCheck(entryItem,entryLog)){
            //初始数据
            if(EntryState.getByType(entryState).equals(EntryState.Init)){
                nextChain.handleEntryItem(entryItem,entryLog);
            }
            //挂起数据
            else if(EntryState.getByType(entryState).equals(EntryState.Hunging)){
                nextChain.handleEntryItem(entryItem,entryLog);
            }
            //错误数据
            else if(EntryState.getByType(entryState).equals(EntryState.ErrorData)){
                if(checkExternalId(entryItem,entryLog)){
                    nextChain.handleEntryItem(entryItem,entryLog);
                }
            }
        }
    }
public class AcctChain extends EntryChain {
    @Override
    public void handleEntryItem(EntryItem entryItem, EntryLog entryLog) {
        HungState hungState = HungState.getByType(entryItem.getHungState());
        if(hungStateCheck(entryItem,entryLog)){
            nextChain.handleEntryItem(entryItem,entryLog);
        }
    }

相关文章

网友评论

      本文标题:责任链模式

      本文链接:https://www.haomeiwen.com/subject/hshfcxtx.html