ACTIVITI 研究代码 之 模版模式
模板方法模式需要开发抽象类和具体子类的设计师之间的协作。一个设计师负责给出一个算法的轮廓和骨架,另一些设计师则负责给出这个算法的各个逻辑步骤。代表这些具体逻辑步骤的方法称做基本方法(primitive method);而将这些基本方法汇总起来的方法叫做模板方法(template method),这个设计模式的名字就是从此而来。
在activit中很多地方用到了此模式,用这个模式可以重用业务逻辑。
实例代码如下:
比如在ACTIVITI 的设置流程变量代码就采用了此模式。
1.抽象模板类。
public abstract class NeedsActiveExecutionCmd<T> implements Command<T>, Serializable {
private static final long serialVersionUID = 1L;
protected String executionId;
public NeedsActiveExecutionCmd(String executionId) {
this.executionId = executionId;
}
public T execute(CommandContext commandContext) {
if(executionId == null) {
throw new ActivitiIllegalArgumentException("executionId is null");
}
ExecutionEntity execution = commandContext
.getExecutionEntityManager()
.findExecutionById(executionId);
if (execution==null) {
throw new ActivitiObjectNotFoundException("execution "+executionId+" doesn't exist", Execution.class);
}
if (execution.isSuspended()) {
throw new ActivitiException(getSuspendedExceptionMessage());
}
return execute(commandContext, execution);
}
/**
* Subclasses should implement this method.
* The provided {@link ExecutionEntity} is guaranteed to be active (ie. not suspended).
*/
protected abstract T execute(CommandContext commandContext, ExecutionEntity execution);
这个代码可以被其他的子类继承,这个类实现了根据executionId获取ExecutionEntity 实例逻辑,其他的子类可以继承这个类,实现 T execute(CommandContext commandContext, ExecutionEntity execution)方法。重用这此逻辑。
子类代码如下:
public class SetExecutionVariablesCmd extends NeedsActiveExecutionCmd<Object> {
private static final long serialVersionUID = 1L;
protected Map<String, ? extends Object> variables;
protected boolean isLocal;
public SetExecutionVariablesCmd(String executionId, Map<String, ? extends Object> variables, boolean isLocal) {
super(executionId);
this.variables = variables;
this.isLocal = isLocal;
}
protected Object execute(CommandContext commandContext, ExecutionEntity execution) {
if (isLocal) {
execution.setVariablesLocal(variables);
} else {
execution.setVariables(variables);
}
// ACT-1887: Force an update of the execution's revision to prevent simultaneous inserts of the same
// variable. If not, duplicate variables may occur since optimistic locking doesn't work on inserts
execution.forceUpdate();
return null;
}
@Override
protected String getSuspendedExceptionMessage() {
return "Cannot set variables because execution '" + executionId + "' is suspended";
}
}
protected Object execute(CommandContext commandContext, ExecutionEntity execution) 这个代码就是子类实现的逻辑。
ACTIVITI 研究代码 之 模版模式的更多相关文章
- 设计模式:模版模式(Template Pattern)-转
模版模式 又叫模板方法模式,在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情冴下,重新定义算法中的某些步骤. 我们使用冲泡咖啡和冲泡茶的例子 加工流程 ...
- 设计模式:模版模式(Template Pattern)
android中的Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益. 模版模式 又叫模板方法模式,在一个方法中定义一个算法的 ...
- 重学 Java 设计模式:实战模版模式「模拟爬虫各类电商商品,生成营销推广海报场景」
作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 黎明前的坚守,的住吗? 有人举过这样一个例子,先给你张北大的录 ...
- 说说设计模式~ 模版模式(Template)
返回目录 模版模式,又被称为模版方法模式,它可以将工作流程进行封装,并且对外提供了个性化的控制,但主流程外界不能修改,也就是说,模版方法模式中,将工作的主体架构规定好,具体类可以根据自己的需要,各自去 ...
- JS模版引擎[20行代码实现模版引擎读后感]
曾经阅读过<只有20行JAVASCRIPT代码, 手把手教你写一个页面模版引擎>这篇文章, 对其中实现模版的想法实在膜拜, 于是有了这篇读后感, 谈谈自己对模版引擎的理解, 以及用自己的语 ...
- 《大话设计模式》ruby版代码:建造者模式
需求: 画一个小人,有头,有身体,两手两脚即可. 初始代码: # -*- encoding: utf-8 -*- #小人一 puts '这是第一个小人' puts '小人一:头' puts '小人一: ...
- 《大话设计模式》ruby版代码:外观模式
需求: 股民买卖股票 初步代码: # -*- encoding: utf-8 -*- #股票1 class Stock1 def buy puts '股票1买入' end def sell puts ...
- PHP面向对象深入研究之【组合模式与装饰模式】
组合模式 定义:组合模式定义了一个单根继承体系,使具有截然不同职责的集合可以并肩工作. 一个军队的案例, <?php abstract class Unit { // 个体 abstract f ...
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_02-vuejs研究-vuejs基础-MVVM模式
1.2.1 MVVM模式 vue.js是一个MVVM的框架,理解MVVM有利于学习vue.js. MVVM拆分解释为: Model:负责数据存储 View:负责页面展示 View Model: ...
随机推荐
- luabind 导出string问题
luabind导出字符串 不能导出char* 会有问题 应该是字符串连接的时候出错了 static _TCHAR* pRetChar = new _TCHAR[10]; memcpy(pRetChar ...
- Spring读书笔记-----Spring的Bean之Bean的基本概念
从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
- [转载] tmux 使用指南
原文: https://danielmiessler.com/study/tmux/ tmux的用法和screen类似, 比screen好用一些, 不过需要单独安装
- mysql 索引的原理
1.考虑下面的情况,mysql> desc student;+----------+-------------+------+-----+---------+-------+| Field | ...
- JavaWeb学习总结(二)—http协议
http协议概念: * 即超文本传输协议.它规定了浏览器与服务器之间的通讯规则. * http是基于请求/响应模式的,所以分为请求协议和响应协议 http的类型: HTTP协议的版本:HTTP/1.0 ...
- (六)C语言之typedef详解
1.typedef可以看作type define的缩写,顾名思义就是类型定义,也就是说它只是给已有的类型重新定义了一个方便使用的别名,并没有产生新的数据类型.typedef的使用与宏定义define有 ...
- golang 资源
1.Learning Go <学习Go语言> http://www.miek.nl/projects/learninggo/中文版http://mikespook.com/learning ...
- Image Cropper+java实现截图工具
首先,请移步http://jquery-plugins.net/image-cropper-jquery-image-cropping-plugin下载iamge cropper的有关js文件及css ...
- J2EE 第二阶段项目之编写代码(三)
我的任务就是项目统计. 1 效益统计 1 教育效益统计表 (教育效益统计表,增,改,查看,查) 2 农牧林效益统计表 (农牧林效益统计表,增,改,查看,查) 3 乡村效益统计表 (乡村效益统计表 ...
- 多语言的sitemap xml
请注意一下 sitemap xml 也有多语言的