Java设计模式相关文章
1. Java设计模式之模板模式
2. Java设计模式之策略模式
3. Java设计模式之工厂模式
4. Java设计模式之装饰者模式
5.Java设计模式之单例模式
Java 设计模式源代码,欢迎star
https://github.com/Dylan-haiji/design-pattern
概念
模板方法(Template Method)模式的定义如下:定义一个操作中的算法骨架,而将算法的一些步骤延迟到子类中,使得子类可以不改变该算法结构的情况下重定义该算法的某些特定步骤。它是一种类行为型模式。
优缺点
-
主要优点如下:
a) 它封装了不变部分,扩展可变部分。它把认为是不变部分的算法封装到父类中实现,而把可变部分算法由子类继承实现,便于子类继续扩展。
b) 它在父类中提取了公共的部分代码,便于代码复用。
c) 部分方法是由子类实现的,因此子类可以通过扩展方式增加相应的功能,符合开闭原则。 -
主要缺点如下:
a) 对每个不同的实现都需要定义一个子类,这会导致类的个数增加,系统更加庞大,设计也更加抽象。
b) 父类中的抽象方法由子类实现,子类执行的结果会影响父类的结果,这导致一种反向的控制结构,它提高了代码阅读的难度。
代码演示
场景设定
详细场景在这里介绍 https://github.com/Dylan-haiji/design-pattern/tree/master/template-mode
结构图
3.1 定义模板接口
public abstract class JavayhTemplate {
private Double height;
private Integer age;
private Double bodyWeight;
private String gender;
private boolean defaultValue = true;
public JavayhTemplate(Double height, Integer age, Double bodyWeight, String gender) {
this.height = height;
this.age = age;
this.bodyWeight = bodyWeight;
this.gender = gender;
}
/**
* @Description 身高要求
* @UserModule: design-pattern
* @author Dylan
* @date 2020/2/7 0007
* @param [height]
* @return java.lang.Integer
*/
public abstract Boolean heightRequirement(Double height);
/**
* @Description 年龄要求
* @UserModule: design-pattern
* @author Dylan
* @date 2020/2/7 0007
* @param [age]
* @return java.lang.Boolean
*/
public abstract Boolean ageRrequirement(Integer age);
/**
* @Description 体重要求
* @UserModule: design-pattern
* @author Dylan
* @date 2020/2/7 0007
* @param [bodyWeight]
* @return java.lang.Boolean
*/
public abstract Boolean bodyWeightRequirement(Double bodyWeight);
/**
* @Description 钩子方法 判断是不是一个女孩
* @UserModule: design-pattern
* @author Dylan
* @date 2020/2/7 0007
* @param [gender]
* @return java.lang.Boolean
*/
public Boolean genderRequirements(String gender){
return Constant.GIRL.equalsIgnoreCase(gender) ? true :false;
}
/**
* @Description 判断是男还是女,这里以女孩为例
* @UserModule: design-pattern
* @author Dylan
* @date 2020/2/7 0007
* @param []
* @return java.lang.Boolean
*/
public /*final*/ Boolean template(){
if(genderRequirements(getGender())){
Boolean heightRequirement = heightRequirement(getHeight());
Boolean ageRrequirement = ageRrequirement(getAge());
Boolean bodyWeightRequirement = bodyWeightRequirement(getBodyWeight());
return heightRequirement.equals(defaultValue) &&
ageRrequirement.equals(defaultValue) &&
bodyWeightRequirement.equals(defaultValue) ? defaultValue : !defaultValue;
}
return !defaultValue;
}
}
3.2 继承模板
public class JimmyService extends JavayhTemplate {
public JimmyService(GirlVO girlVO) {
super(girlVO.getHeight(),girlVO.getAge(),girlVO.getBodyWeight(),girlVO.getGender());
}
@Override
public Boolean heightRequirement(Double height) {
return height > 165;
}
@Override
public Boolean ageRrequirement(Integer age) {
return age < 28;
}
@Override
public Boolean bodyWeightRequirement(Double bodyWeight) {
return bodyWeight < 50;
}
}
3.3 应用
@RestController
public class GirlWeb {
/**
* @Description 判断这个女孩符合你的要求与否
* @UserModule: design-pattern
* @author Dylan
* @date 2020/2/7 0007
* @param [girlVO]
* @return com.javayh.result.DataResult
*/
@PostMapping(value = "getGirl")
public DataResult getGirl(@RequestBody GirlVO girlVO){
JavayhTemplate jimmyService = new JimmyService(girlVO);
Boolean template = jimmyService.template();
return DataResult.success(template == true ? "我很心仪!" : "我们不合适");
}
}
3.5 验证
访问 http://localhost:8093/template/getGirl
关注 Java有货领取更多资料
联系小编。微信:372787553,带您进群互相学习
左侧小编微信,右侧获取免费资料
- Java 设计模式学习代码 https://github.com/Dylan-haiji/design-pattern
- SpringCloud学习代码: https://github.com/Dylan-haiji/javayh-cloud
- AlibabaCloud学习代码:https://github.com/Dylan-haiji/javayh-cloud-nacos
- SpringBoot+Mybatis 多数据源切换:https://github.com/Dylan-haiji/javayh-boot-data-soure
- Redis、Mongo、Rabbitmq、Kafka学习代码: https://github.com/Dylan-haiji/javayh-middleware
- SpringBoot+SpringSecurity实现自定义登录学习代码:https://github.com/Dylan-haiji/javayh-distribution