模板方法模式需要开发抽象类和具体子类的设计师之间的协作。一个设计师负责给出一个算法的轮廓和骨架,另一些设计师则负责给出这个算法的各个逻辑步骤。代表这些具体逻辑步骤的方法称做基本方法(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. VS2013常用快捷方式

    [原文出处]http://blog.csdn.net/lushuner/article/details/23688629 1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合 ...

  2. centos中更换jdk的版本

    现在讲的是Linux中更换jdk版本的问题,卸载Linux自带的jdk更换sun的jdk百度一大堆,但是如果我安装的sun的jdk是1.7的想更换到1.8的如何解决呢,方法其实超easy. 把1.8的 ...

  3. Ext.net 异常统一管理,铥掉可恶的 Request Failure

    Ext.net 异常统一管理,铥掉可恶的 Request Failure 看着这样的框框是不是很不爽 灭他.也不难.. .如果全部页面都有继承一个自定义的父类 ..那整个项目代码量就只有几行了.. 单 ...

  4. SAP FI/CO凭证不一致的解决办法

    First, use program RKACOR20 to delete the incorrect CO documents. OKBA - Transfer FI Documents to CO ...

  5. 关于Spring定时任务(定时器)用法

    Spring定时任务的几种实现 Spring定时任务的几种实现 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): 从作业类的继承方式来讲,可以分为两类: 从任务调度的触发时机来 ...

  6. Eclipse中导入外部jar包(zhuan)

    http://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html ************************************* ...

  7. HA功能中ZKFC对NN状态的控制

    ZKFC : zookeeper FailoverController NN : name node Hadoop 2.0 HA架构图: FC是要和NN一一对应的,两个NN就要部署两个FC.它负责监控 ...

  8. maven和jdk版本不匹配

    解决方法:http://blog.csdn.net/mafan121/article/details/51944346

  9. BOM头的来源

    类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM).它是一串隐藏的字符,用于让记事本等编辑器识别 ...

  10. 使用fragmenttabhost后,子fragment怎么获取ID?怎么用getSharedPreferences

    使用fragmenttabhost后,子fragment怎么获取ID?怎么用getSharedPreferences public View onCreateView(LayoutInflater i ...