Apache Commons Chain(2) -开源责任链模式(apache commons chain)


前一篇 文章,我们大概介绍了 Apache commons chain 及完成了一个简单的demo ,今天我们就从 源码级别来分析下,到底是一个什么样的执行流程 ~~~~!

首先从执行入口类:

public class CommondChain extends ChainBase {

    public CommondChain() {
        super();
        addCommand(new GetUserInfo());
        addCommand(new TestDriver());
        addCommand(new NegotiateSale());
        addCommand(new ArrangeFinancing());
        addCommand(new CloseSale());
    }

    public static void main(String[] args) {
        CommondChain commondChain = new CommondChain();
        Context context = new ContextBase();

        try {
            commondChain.execute(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我们从两个方面来进行源码解析:

1、实现了Command 的业务功能如何加入容器中?加入到什么容器中?

具体实现 在 ChainBase.class

   public void addCommand(Command command) {

        if (command == null) {
            throw new IllegalArgumentException();
        }
        if (frozen) {
            throw new IllegalStateException();
        }
        Command[] results = new Command[commands.length + 1];
        System.arraycopy(commands, 0, results, 0, commands.length);
        results[commands.length] = command;
        commands = results;

    }

其中,commands 为数组类型。说白了就是加入一个业务处理command 就追加到数组里面去。

如何执行这个流程呢?

具体调用也是在 ChainBase.class

  public boolean execute(Context context) throws Exception {

        // Verify our parameters
        if (context == null) {
            throw new IllegalArgumentException();
        }

        // Freeze the configuration of the command list
        frozen = true;

        // Execute the commands in this list until one returns true
        // or throws an exception
        boolean saveResult = false;
        Exception saveException = null;
        int i = 0;
        int n = commands.length;
        for (i = 0; i < n; i++) {
            try {
                saveResult = commands[i].execute(context);
                if (saveResult) {
                    break;
                }
            } catch (Exception e) {
                saveException = e;
                break;
            }
        }

        // Call postprocess methods on Filters in reverse order
        if (i >= n) { // Fell off the end of the chain
            i--;
        }
        boolean handled = false;
        boolean result = false;
        for (int j = i; j >= 0; j--) {
            if (commands[j] instanceof Filter) {
                try {
                    result =
                        ((Filter) commands[j]).postprocess(context,
                                                           saveException);
                    if (result) {
                        handled = true;
                    }
                } catch (Exception e) {
                      // Silently ignore
                }
            }
        }

        // Return the exception or result state from the last execute()
        if ((saveException != null) && !handled) {
            throw saveException;
        } else {
            return (saveResult);
        }

    }

看下 代码实现思路还是很简单的哈~~

  1. 定义一个基类
  2. 业务逻辑分别实现该基类
  3. 迭代该数组集合类
  4. 调用业务类实现方法,完成顺序一个执行流程

Filter 部分代码逻辑,同理,自行去分析看下了~~~

----------------------研究源码最好的方式就是 Debug

展开阅读全文

页面更新:2024-03-11

标签:数组   说白了   容器   源码   顺序   逻辑   入口   思路   流程   定义   级别   类型   模式   代码   简单   业务   责任   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top