首页 > 应用开发 > Java > 设计模式之模板方法模式
模板方法模式是一种行为型设计模式,它定义一个操作(模板方法)的基本组合与控制流程,将一些步骤(抽象方法)推迟到子类中,在使用时调用不同的子类,就可以达到不改变一个操作的基本流程情况下,即可修改其中的某些特定步骤。这种设计方式将特定步骤的具体实现与操作流程分离开来,实现了代码的复用和扩展,从而提高代码质量和可维护性。
模板方法模式包含以下:
模板方法模式的优点:
模板方法模式的缺点:
简单列一些模板方法模式的应用场景:
Spring 中的
JdbcTemplate、RestTemplate、RabbitTemplate、KafkaTemplate 等。如上,我们用一个简单的发送短信代码来做模板方法模式的示例:
定义一个发送短信模板
/**
* 发送短信模板
*/
public
abstract
class
SmsTemplate {
/**
* 发送方法
*
*
@param mobile 手机号
*/
public
void
send
(String mobile)
throws Exception {
System.out.println(
"检查用户一分钟内是否发送过短信,
mobile:" + mobile);
if (checkUserReceiveInOneMinute(mobile)) {
throw
new Exception(
"请等待1分钟后重试");
}
String code = genCode();
if (manufacturer(mobile, code)) {
System.out.println(
"短信厂商发送短信成功,
mobile:" + mobile +
",code=" + code);
save2redis(mobile, code);
}
}
/**
* 模板方法,由不同的厂商来实现发送短信到手机上
*
@return
*/
abstract
boolean
manufacturer
(String mobile, String code);
/**
* 检查1分钟内该手机号是否接收过验证码,1分钟内接收过就不能在发送验证码
*
@param mobile
*
@return
*/
public
boolean
checkUserReceiveInOneMinute
(String mobile) {
return ...;
}
/**
* 生成6位验证码
*
@return
*/
public String
genCode
() {
return
"123456";
}
/**
* 将手机号+验证码存进redis中,给登录接口做校验用
*
@param mobile
*
@param code
*/
public
void
save2redis
(String mobile, String code) {
...
}
}
添加两个不同厂商实现的子类
/**
* 阿里云短信发送
*/
public
class
AliyunSmsSend
extends
SmsTemplate{
@Override
boolean
manufacturer
(String mobile, String code) {
System.out.println(
"读取阿里云短信配置");
System.out.println(
"创建阿里云发送短信客户端");
System.out.println(
"阿里云发送短信成功");
return
true;
}
}
/**
* 腾讯云短信发送
*/
public
class
TencentSmsSend
extends
SmsTemplate {
@Override
boolean
manufacturer
(String mobile, String code) {
System.out.println(
"读取腾讯云短信配置");
System.out.println(
"创建腾讯云发送短信客户端");
System.out.println(
"腾讯云发送短信成功");
return
true;
}
}
在 Java 程序中进行调用
public
class
Main {
public
static
void
main
(String[] args)
throws Exception {
SmsTemplate smsTemplate1 =
new AliyunSmsSend();
smsTemplate1.send(
"13333333333");
System.out.println(
"---------------------------");
SmsTemplate smsTemplate2 =
new TencentSmsSend();
smsTemplate2.send(
"13333333333");
}
}
输出如下:
检查用户一分钟内是否发送过短信,mobile:13333333333
读取阿里云短信配置
创建阿里云发送短信客户端
阿里云发送短信成功
短信厂商发送短信成功,mobile:13333333333,code=123456
---------------------------
检查用户一分钟内是否发送过短信,mobile:13333333333
读取腾讯云短信配置
创建腾讯云发送短信客户端
腾讯云发送短信成功
短信厂商发送短信成功,mobile:13333333333,code=123456
我们来看看模板方法模式的组成:
SmsTemplate 中定义了发送短信的基本流程操作
AliyunSmsSend、TencentSmsSend 继承抽象类,实现抽象方法
manufacturer(String mobile, String code),定义流程中的可变部分。send(mobile) ,在模板方法中完成了基本流程组合与条件控制。在
Spring 中实现模板方法模式,是非常简单的,我们只需要对上述的
Java 代码示例的
AliyunSmsSend 类稍作改造,加上
@Component 注解就行,
/**
* 阿里云短信发送
*/
@Component
public
class
AliyunSmsSend
extends
SmsTemplate{
@Override
boolean
manufacturer
(String mobile, String code) {
IUserService userService = SpringUtil.getBean(IUserService
.
class);
System.out.println(
"读取阿里云短信配置");
System.out.println(
"创建阿里云发送短信客户端");
System.out.println(
"阿里云发送短信成功");
return
true;
}
}
如果在
AliyunSmsSend 类中需要注入其他
bean,通过
cn.hutool.extra.spring.SpringUtil.getBean(...) 方法获取对应
bean 就行。
在Java8 中,还可以使用函数表达式来替换抽象方法,代码如下,
/**
* 发送短信模板
*/
public
class
SmsTemplateLambda {
/**
* 发送短信
*
@param mobile 手机号
*
@param biFunction
*
@throws Exception
*/
public
void
send
(String mobile,
BiFunction<String, String, Boolean> biFunction)
throws Exception {
System.out.println(
"检查用户一分钟内是否发送过短信,mobile:" + mobile);
if (checkUserReceiveInOneMinute(mobile)) {
throw
new Exception(
"请等待1分钟后重试");
}
String code = genCode();
if (biFunction.apply(mobile, code)) {
System.out.println(
"短信厂商发送短信成功,mobile:"
+ mobile +
",code=" + code);
save2redis(mobile, code);
}
}
...
}
通过
BiFunction 函数,将不同厂商发送短信到用户手机的代码在
send(mobile) 方法中分离处理。
调用方法如下:
public
static
void
main
(String[] args)
throws Exception {
SmsTemplateLambda smsTemplateLambda =
new SmsTemplateLambda();
smsTemplateLambda.send(
"1333333333", (s, s2) -> {
System.out.println(
"读取阿里云短信配置");
System.out.println(
"创建阿里云发送短信客户端");
System.out.println(
"阿里云发送短信成功");
return
true;
});
smsTemplateLambda.send(
"1333333333", (s, s2) -> {
System.out.println(
"读取腾讯云短信配置");
System.out.println(
"创建腾讯云发送短信客户端");
System.out.println(
"腾讯云发送短信成功");
return
true;
});
}
可以看到,我们可以只在调用
SmsTemplateLambda 类的
send(mobile) 方法时,才实现不同厂商发送短信到手机的具体逻辑。好处就是每增加一个模板方法时,不用增加具体的子类实现,减少类的创建与降低子类的实现成本。
模板方法模式通过定义一个流程基本操作也就是模板方法,将具体的实现步骤推迟到子类中,使得子类可以灵活地实现可变的行为,这是模板方法模式的核心思想与价值所在。
关注公众号【waynblog】每周分享技术干货、开源项目、实战经验、高效开发工具等,您的关注将是我的更新动力!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70029997/viewspace-2955404/ ,如需转载,请注明出处,否则将追究法律责任。