模板方法模式需要开发抽象类和具体子类的设计师之间的协作。一个设计师负责给出一个算法的轮廓和骨架,另一些设计师则负责给出这个算法的各个逻辑步骤。代表这些具体逻辑步骤的方法称做基本方法(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 研究代码 之 模版模式的更多相关文章

  1. 设计模式:模版模式(Template Pattern)-转

    模版模式 又叫模板方法模式,在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情冴下,重新定义算法中的某些步骤. 我们使用冲泡咖啡和冲泡茶的例子 加工流程 ...

  2. 设计模式:模版模式(Template Pattern)

    android中的Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益. 模版模式 又叫模板方法模式,在一个方法中定义一个算法的 ...

  3. 重学 Java 设计模式:实战模版模式「模拟爬虫各类电商商品,生成营销推广海报场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 黎明前的坚守,的住吗? 有人举过这样一个例子,先给你张北大的录 ...

  4. 说说设计模式~ 模版模式(Template)

    返回目录 模版模式,又被称为模版方法模式,它可以将工作流程进行封装,并且对外提供了个性化的控制,但主流程外界不能修改,也就是说,模版方法模式中,将工作的主体架构规定好,具体类可以根据自己的需要,各自去 ...

  5. JS模版引擎[20行代码实现模版引擎读后感]

    曾经阅读过<只有20行JAVASCRIPT代码, 手把手教你写一个页面模版引擎>这篇文章, 对其中实现模版的想法实在膜拜, 于是有了这篇读后感, 谈谈自己对模版引擎的理解, 以及用自己的语 ...

  6. 《大话设计模式》ruby版代码:建造者模式

    需求: 画一个小人,有头,有身体,两手两脚即可. 初始代码: # -*- encoding: utf-8 -*- #小人一 puts '这是第一个小人' puts '小人一:头' puts '小人一: ...

  7. 《大话设计模式》ruby版代码:外观模式

    需求: 股民买卖股票 初步代码: # -*- encoding: utf-8 -*- #股票1 class Stock1 def buy puts '股票1买入' end def sell puts ...

  8. PHP面向对象深入研究之【组合模式与装饰模式】

    组合模式 定义:组合模式定义了一个单根继承体系,使具有截然不同职责的集合可以并肩工作. 一个军队的案例, <?php abstract class Unit { // 个体 abstract f ...

  9. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_02-vuejs研究-vuejs基础-MVVM模式

    1.2.1 MVVM模式 vue.js是一个MVVM的框架,理解MVVM有利于学习vue.js.   MVVM拆分解释为:   Model:负责数据存储 View:负责页面展示 View Model: ...

随机推荐

  1. MyEclipse JCO tomcat 提示查找不到sapjco3.dll

    java.lang.UnsatisfiedLinkError: no sapjco3 in java.library.path 1.system32添加sapjco3.dll 2.tomcat bin ...

  2. 内部使用final参数的原因

    局部内部类(即:定义在方法中的内部类),访问方法中的局部变量 : 局部变量的生命周期与对象的生命周期的不一致性!方法在执行完方法的局部变量就消失,而内部类如果有引用还是存在的,那么将找不到变量.此时设 ...

  3. Mysql 命令行工具

    1.Mysql命令行工具分为两类:服务端命令行工具和客户端命令行工具. 2.服务端工具 mysql_install_db:建库工具 mysqld_safe:Mysql服务的启动工具,mysqld_sa ...

  4. linux之echo命令

    linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个提示 ...

  5. Linux进程空间分布 & 进程控制块 PCB

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Verdana; color: #555555 } span.s1 { } Linux使用两级 ...

  6. jQuery动态加载css文件实现方法

    $("<link>").attr({ rel: "stylesheet",type: "text/css",href: &quo ...

  7. 【ufldl tutorial】Convolution and Pooling

    卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...

  8. SDL2.0的加载图片贴图

    加载图片贴图,采用了SDL_Window.SDL_Renderer.SDL_Texture和SDL_Image库 实例: #include <stdio.h> #include <m ...

  9. 1.2G内存试玩RAMOS_XP

    1.2G内存试玩RAMOS_XP1.为了防止做系统时出现意外,用Bootice把C盘MBR修改为Grub4dos,这样子系统如果失败,可以进入PE重做. 2.进入PE格式化C盘,格式化的时候勾选启用N ...

  10. ios CoreBluetooth 警告 is being dealloc'ed while pending connection

    ios CoreBluetooth 警告 is being dealloc'ed while pending connection CoreBluetooth[WARNING] <CBPerip ...