扯一扯命令模式

老早就看过命令模式,但是具体项目使用起来,理解的不是很好,最近有点小感悟,闲扯下
命令模式的核心:包装命令处理作为一个类存在 --core1
             提供统一接口--core2

一般情景中能看到的命令模式:
    struts2就是基于命令模式的框架,请求--响应这种web方式,一个path对应一个action处理
path就是命令,对应的action就是命令,而且还提供了统一的接口exe
但是通常用插件实现零配置,这个用annotation方式也实现了统一的处理,具体没细看
public interface Action {  
  
    /** 
     * Where the logic of the action is executed. 
     * 
     * @return a string representing the logical result of the execution. 
     *         See constants in this interface for a list of standard result values. 
     * @throws Exception thrown if a system level exception occurs. 
     *                   Application level exceptions should be handled by returning 
     *                   an error value, such as Action.ERROR. 
     */  
    public String execute() throws Exception;  
}  





    c/s这种开发模式的命令模式,菜单处理操作对应于  事件--响应 这种跟请求--响应 类似
一个事件对应一个命令类的处理,并且都是提供统一接口来包装持有这些命令
详细分析:
命令类--command
调用者(持有命令类)--invoke
命令模式就位具体事件或者请求之类的,会给他生成一个具体调用者实例,这个实例是根据请求的特性

    命令模式可以把这些命令封装起来,实现撤销,重做等方式,这个就是复杂的命令模式实现,简单的话只需在持有类中有一个记录命令操作的历史类

猜你喜欢

转载自blognojava.iteye.com/blog/1283139