目录
- 一、简介
- 二、模板方法模式
- 2.1、抽象模板类
- 2.2、退款申请
- 2.3、转账申请
- 2.4、使用
- 三、优点与缺点
一、简介
模板方法模式 又称 模板模式(Template Pattern)是一种行为设计模式,它定义了一个算法的框架,并允许子类重写特定步骤的方法,而不改变算法的整体结构。
在模板模式中,定义了一个抽象类,该抽象类包含了算法的框架,其中的某些步骤由抽象方法或默认实现提供。具体步骤的实现延迟到子类中。这样,子类可以根据需要重写父类的方法,同时保留算法的整体结构。模板模式主要包含以下关键组件:
- 模板(Abstract Template): 定义了算法的框架,通常是一个抽象类,其中包含了算法中的各个步骤,有些步骤可能是抽象的或有默认实现。
- 具体实现(Concrete Implementation): 继承自模板类的具体子类,实现了在模板中定义的抽象方法,以完成算法中的特定步骤。
- 钩子方法(Hooks): 可选的方法,可以在模板类中提供默认实现,但子类可以选择性地覆盖它们来实现特定行为。
模板模式使得整体算法的结构固定,但其中的某些步骤可以由子类进行定制,从而避免了代码重复和增加灵活性,使得代码更易于维护和扩展。
二、模板方法模式
2.1、抽象模板类
import java.util.Map;public abstract class BusinessProcessTemplate {public final Map<String, String> process(Map<String, String> paramsMap) {// 检查参数Map<String, String> validateResult = validate(paramsMap);String code = validateResult.getOrDefault("code", "0000");System.out.println("校验参数返回:"+validateResult);if (!"0000".equals(code)) {return validateResult;}// 执行业务Map<String, String> resultMap = execute(paramsMap);System.out.println("业务处理返回结果:"+resultMap);// 省略其他操作及其异常处理// 返回结果return resultMap;}public abstract Map<String, String> validate(Map<String, String> paramsMap);public abstract Map<String, String> execute(Map<String, String> paramsMap);}
2.2、退款申请
import java.util.HashMap;
import java.util.Map;public class RefundApplyService extends BusinessProcessTemplate {@Overridepublic Map<String, String> validate(Map<String, String> paramsMap) {System.out.println("-------------------------退款申请---------------------");System.out.println("退款申请参数:" + paramsMap);Map<String, String> validateResult = new HashMap<>();validateResult.put("code", "1000");int amount = Integer.parseInt(paramsMap.getOrDefault("amount", "0"));if (amount <= 0) {validateResult.put("msg", "退款金额必须大于0元");return validateResult;}// 其他参数校验等...validateResult.put("code", "0000");validateResult.put("msg", "参数验证通过");return validateResult;}@Overridepublic Map<String, String> execute(Map<String, String> paramsMap) {Map<String, String> resultMap = new HashMap<>();// 执行业务判断// 退款申请等resultMap.put("code", "0000");resultMap.put("msg", "success");return resultMap;}
}
2.3、转账申请
import org.apache.commons.lang3.StringUtils;import java.util.HashMap;
import java.util.Map;public class TransferApplyService extends BusinessProcessTemplate{@Overridepublic Map<String, String> validate(Map<String, String> paramsMap) {System.out.println("------------------------转账申请----------------------");System.out.println("转账申请参数:"+paramsMap);Map<String, String> validateResult = new HashMap<>();validateResult.put("code", "1000");String account = paramsMap.getOrDefault("account", "0");int amount = Integer.parseInt(paramsMap.getOrDefault("amount", "0"));if (StringUtils.isNotBlank(account)) {validateResult.put("msg", "账户不能为空");return validateResult;}if (amount <= 0 || amount>200) {validateResult.put("msg", "转账金额必须大于0元,小于等于200元");return validateResult;}// 其他参数校验等...validateResult.put("code", "0000");validateResult.put("msg", "参数验证通过");return validateResult;}@Overridepublic Map<String, String> execute(Map<String, String> paramsMap) {Map<String, String> resultMap = new HashMap<>();// 执行业务判断// 转账申请等resultMap.put("code", "0000");resultMap.put("msg", "success");return resultMap;}
}
2.4、使用
import java.util.HashMap;
import java.util.Map;public class TemplatePatternExample {public static void main(String[] args) {// 退款申请Map<String, String> refundParamsMap = new HashMap<>();refundParamsMap.put("amount", "100");BusinessProcessTemplate refundProcess = new RefundApplyService();refundProcess.process(refundParamsMap);// 转账申请Map<String, String> transferParamsMap = new HashMap<>();transferParamsMap.put("amount", "300");BusinessProcessTemplate transferProcess = new TransferApplyService();transferProcess.process(transferParamsMap);}
}
三、优点与缺点
模板方法模式(Template Method Pattern)的优点和缺点如下所示:
优点
- 代码复用: 将相同的代码抽取到父类的模板方法中,实现代码复用,避免了重复编码。
- 提高扩展性: 允许子类重写父类的特定步骤,提高了系统的灵活性和可扩展性。
- 符合开闭原则: 模板方法模式使得父类的模板方法结构不变,子类可以在不改变整体结构的情况下修改特定步骤的实现,符合开闭原则。
- 封装不变部分: 将算法的不变部分封装到父类中,便于管理和维护。
缺点
- 父类对子类的依赖: 子类必须遵循父类的约定来实现特定的步骤,限制了子类的灵活性。
- 增加了类的数量: 对每个需要定制的算法步骤都需要一个子类,可能会导致类的数量增加。
- 不适用于所有场景: 如果算法的整体结构经常变化,或者每个子类的差异性较大,模板方法可能不适用。
总的来说,模板方法模式能够提高代码的复用性、提高系统的扩展性和可维护性,但需要注意子类的依赖性和不适用于所有的场景。选择使用模板方法模式时,需要根据具体的情况来评估其优缺点,以确保模式的适用性。