设计模式之Command
来源:互联网 发布:火方言版网络原唱 编辑:程序博客网 时间:2024/06/16 13:00
将客户端的请求传入一个对象,无需了解请求激活的动作或相关请求的处理细节,这样可以解耦发送者和接收者之间的联系,发送者调用一个操作,接收者接受操作请求执行相关的操作,这就是Command设计模式。将请求的命令封装在一个类中,然后调用者再对这个类进行操作,换句话说,本来是调用者是直接调用这些命令的,使用Command模式设计,在这两者之间添加一个中间者, 这样将直接关系就切断了,这样符合封装的特性,降低耦合度。
一、实现
典型的Command模式,需要有一个接口,调用者直接调用接口,接口的真正实现由另外的实现类来操作具体的细节。
接口:
public interface ContextListenerWare {public void initialized(ServletContextEvent event);public void destroyed(ServletContextEvent event);}实现类:
public class DictService implements ContextListenerWare{@Overridepublic void initialized(ServletContextEvent event) {loadingDicts();}@Overridepublic void destroyed(ServletContextEvent event) {unLoadingDicts();} ... }调用类:
public class InitContextListener implements ServletContextListener{private String clazz = getClass().getName();private Logger logger=CommonLogger.logger;private static final Set<ContextListenerWare> initors;static{initors = new HashSet<ContextListenerWare>();}@Overridepublic void contextInitialized(ServletContextEvent event) {initors.add(new DictService()); //多个ContextListenerWare的实现类的话,往initors里添加 ContextListenerWare initor = null;for(Iterator<ContextListenerWare> it = initors.iterator(); it.hasNext() ;){initor = it.next();try{if(initor != null){initor.initialized(event);}}catch(Exception e){logger.error(clazz+":call "+initor.getClass().getName()+".initialized,occurs error:",e);}}}@Overridepublic void contextDestroyed(ServletContextEvent event) {ContextListenerWare initor = null;for(Iterator<ContextListenerWare> it = initors.iterator(); it.hasNext() ;){initor = it.next();try{if(initor != null){initor.destroyed(event);}}catch(Exception e){logger.error(clazz+":call "+initor.getClass().getName()+".destroyed,occurs error:",e);}}}}
0 0
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之 Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之 command
- 设计模式之 command
- 设计模式之Command
- 设计模式之--Command
- 设计模式之Command
- 设计模式之Command
- 设计模式之Command
- explicit constructor(显示构造函数)
- openfire单独编译插件
- Python中的random模块
- CocoaPods详解之----使用篇
- 【足迹C++primer】40、动态数组
- 设计模式之Command
- C 输入10个数,将最小的与第一个换,最大的与最后一个换
- 同步和异步
- android的消息处理机制(图+源码分析)——Looper,Handler,Message
- hdu 3709 Balanced Number
- 依赖注入及企业库Unity组件学习笔记
- background-position css图片平铺
- 2020 绝对值排序
- java python oracle判断字符串是否为数字的函数