ITeye博客

apache commons chain

SmallMin · 2011-08-17 22:07 · java · 编程语言 · Markdown

Chain of Responsibility(CoR)模式也叫职责链模式或者职责连锁模式,是由GoF提出的23种软件设计模式的一种。Chain of Responsibility模式是行为模式之一,该模式构造一系列分别担当不同的职责的类的对象来共同完成一个任务,这些类的对象之间像链条一样紧密相连,所以被称作职责链模式.

将CoR和Command模式结合使用,可以使得客户端在处理的过程不需要关心是使用一个command还是 一系列的command。通过 Liskov 代换原则,chain implement command,在使用command的地方都可以使用chain。

apache commons chain 提供了对CoR模式的基础支持,简化了和促进了实际应用CoR模式。

apache commons chain 核心组件 1.Context command的执行的上下文环境,即包含了应用程序的状态。 extends map。继承自map接口,没有增加任何其他的接口方法,它只是一个标记接口。ContextBase是它的实现类。 一般情况下,使用map来记录状态就可以了。例如map.put(state,value),map.get(state).在某些特定的使用时候,若需要明确定义拥有特定状态属性的java bean,例如通过ProfileContext.getXXState()/ProfileContext.setXXState(stateValue)来获得和属性具体的属性值。通过继承ContextBase 来实现,加入特定的属性字段,与此同时获得了两种方式来获取具体的状态。get(state)和getXXState()。通过反射类实现。


public class ContextBase extends HashMap implements Context {
    public Object put(Object key, Object value) {

        // Case 1 -- no local properties
        if (descriptors == null) {
            return (super.put(key, value));
        }

        // Case 2 -- this is a local property
        if (key != null) {
            PropertyDescriptor descriptor =
                (PropertyDescriptor) descriptors.get(key);
            if (descriptor != null) {
                Object previous = null;
                if (descriptor.getReadMethod() != null) {
                    previous = readProperty(descriptor);
                }
                writeProperty(descriptor, value);
                return (previous);
            }
        }

        // Case 3 -- store or replace value in our underlying map
        return (super.put(key, value));

    }

    public Object get(Object key) {

        // Case 1 -- no local properties
        if (descriptors == null) {
            return (super.get(key));
        }

        // Case 2 -- this is a local property
        if (key != null) {
            PropertyDescriptor descriptor =
                (PropertyDescriptor) descriptors.get(key);
            if (descriptor != null) {
                if (descriptor.getReadMethod() != null) {
                    return (readProperty(descriptor));
                } else {
                    return (null);
                }
            }
        }

        // Case 3 -- retrieve value from our underlying Map
        return (super.get(key));

    }
}

2.Command:职责的最小单元。


public interface Command {
....
    boolean execute(Context context) throws Exception;
....
}

return false 会继续执行chain中后续的command,return true就不会了。

3.Chain chain of command 实现源码


public class ChainBase implements Chain {

      public void addCommand(Command command) {

        if (command == null) {
            throw new IllegalArgumentException();
        }
        if (frozen) {//execute method will freeze the configuration of the command list
            throw new IllegalStateException();
        }
        Command[] results = new Command[commands.length + 1];
        System.arraycopy(commands, 0, results, 0, commands.length);
        results[commands.length] = command;
        commands = results;

    }

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) {//false继续执行chain下面的command,true 就直接返回不再执行了其他command
                    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) {//filter定义了postprocess方法,用于自定义处理结束执行资源释放的操作
                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);
        }

    }

}

3.filter:是一种特殊的command,加入了postprocess 方法。在chain return之前会去执行postprocess。可以在该方法中释放资源。

4.catalog:command或chain的集合。通过配置文件类加载chain of command 或者command。通过catalog.getCommand(commandName)获取Command。


<catalog>
  <chain name="LocaleChange">
    <command 
      className="org.apache.commons.chain.mailreader.commands.ProfileCheck"/>
    <command 
      className="org.apache.commons.chain.mailreader.commands.LocaleChange"/>
  </chain>
  <command 
    name="LogonUser" 
    className="org.apache.commons.chain.mailreader.commands.LogonUser"/>
</catalog>


boolean executeCatalogCommand(Context context,
            String name, HttpServletRequest request) 
        throws Exception {
    
        ServletContext servletContext =
                request.getSession().getServletContext();  
        Catalog catalog =
                (Catalog) servletContext.getAttribute("catalog");
        Command command = catalog.getCommand(name);
        boolean stop = command.execute(context);
        return stop;
        
    } 

在web环境使用还需要配置ChainListener


<!-- Commons Chain listener to load catalogs  -->
<context-param>
  <param-name>org.apache.commons.chain.CONFIG_CLASS_RESOURCE</param-name>
  <param-value>resources/catalog.xml</param-value>
</context-param>
<listener>
  <listener-class>org.apache.commons.chain.web.ChainListener</listener-class>
</listener>

在ChainListener监听器init中完成加载catalog的工作,主要是使用Digester解析catalog.xml来得到catalog对象,并放入ServletContext 中去。

commons chain 还提供了ServletWebContext,从名字就可以知道,这是servlet环境下的context实现。另外还有PortletWebContext ,FacesWebContext。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。