以支付业务开发为例,在日常需求不断的迭代过程中,你会发现所有支付服务类中包含许多相似代码。 尽管这些类处理请求第三方支付平台的代码完全不同, 但参数校验、创建订单等的代码却几乎完全一样。 如何在保持业务逻辑完整的情况下去除重复代码?


模板方法设计模式是一种行为设计模式, 它在一个方法中定义一个算法的骨架,而把一些步骤延迟到子类。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。

如上图所示,建房子的流程(打地基 → 立框架→装门窗→封屋顶等)是通用的,但细节可以随着房型不同而变化。

抽象类 会声明作为算法步骤的方法, 以及依次调用它们的实际模板方法。 算法步骤可以被声明为抽象类型, 也可以提供一些默认实现。
具体类 可以重写所有步骤, 但不能重写模板方法自身。
优点
缺点



分析
咖啡和茶的冲泡流程相似(煮水→冲泡→倒杯→加调料),仅 “冲泡” 和 “加调料” 步骤不同。
结构
抽象类(CaffeineBeverage):定义模板方法(prepareRecipe()),包含算法的固定步骤(boilWater()、pourInCup())和抽象步骤(brew()、addCondiments())。
具体子类(Coffee、Tea):实现抽象步骤(Coffee的brew()是 “冲泡咖啡粉”,Tea的brew()是 “浸泡茶叶”)。
System.Data.Common.DbCommand类(数据库操作体系) DbCommand是.NET 中所有数据库命令对象的抽象基类(如SqlCommand、OracleCommand),其定义了数据库命令执行的模板流程ExecuteReader()(执行查询并返回数据阅读器),而具体的数据库交互逻辑(如发送 SQL 到数据库、接收返回结果)则通过抽象方法ExecuteDbDataReader(CommandBehavior behavior)延迟到子类实现
public abstract class DbCommand : IDisposable
{
public DbDataReader ExecuteReader()
{
// 模板方法定义执行逻辑框架
return ExecuteDbDataReader(CommandBehavior.Default);
}
protected abstract DbDataReader ExecuteDbDataReader(CommandBehavior behavior);
}消息通知服务使用模板方法设计模式实现
需求
- 需要支持发短信通知、邮件通知、微信公众号通知。
- 核心流程包括校验参数(VerifyParam)、获取平台账号信息(GetPlatformAccount)、构造消息内容(BuildMsgContent)、发送消息(SendMsg)
UML类图

// 抽象基类
public abstract class BaseNotificationService
{
// 模板方法
public string SendNotification(NotificationParam param)
{
VerifyParam(param);
var account = GetPlatformAccount();
var content = BuildMsgContent(param);
return SendMsg(account, content);
}
// 校验参数(默认实现,子类可重写)
protected virtual void VerifyParam(NotificationParam param)
{
// 通用参数校验逻辑
}
// 获取平台账号信息(默认实现,子类可重写)
protected virtual PlatformAccount GetPlatformAccount()
{
// 通用账号获取逻辑
}
// 构造消息内容(默认实现,子类可重写)
protected virtual string BuildMsgContent(NotificationParam param)
{
// 通用消息构造逻辑
}
// 发送消息(抽象方法,子类必须实现)
protected abstract string SendMsg(PlatformAccount account, string content);
}
// 短信通知子类
public class SmsNotificationService : BaseNotificationService
{
protected override string SendMsg(PlatformAccount account, string content)
{
// 调用短信平台接口发送消息
return "短信发送成功";
}
}
// 邮件通知子类
public class EmailNotificationService : BaseNotificationService
{
protected override string SendMsg(PlatformAccount account, string content)
{
// 调用邮件平台接口发送消息
return "邮件发送成功";
}
}
// 微信公众号通知子类
public class WeChatNotificationService : BaseNotificationService
{
protected override string SendMsg(PlatformAccount account, string content)
{
// 调用微信公众号接口发送消息
return "微信公众号消息发送成功";
}
}
// 辅助类
public class NotificationParam
{
// 通知相关参数(如接收人、消息主题、消息内容等)
}
public class PlatformAccount
{
// 平台账号信息(如账号、密码、接口地址等)
}