当前位置:网站首页>(2022.1.31-2022.2.14) template mode analysis
(2022.1.31-2022.2.14) template mode analysis
2022-04-22 05:16:00 【Look, water is water】
In template mode (Template Pattern) in , An abstract class publicly defines how to execute its methods / Templates . Its subclass can override method implementation as needed , But the call will take place in the way defined in the abstract class . This type of design pattern belongs to behavioral pattern .
Introduce
Intention : Defines the skeleton of an algorithm in an operation , Instead, defer some steps to subclasses . The template approach allows subclasses to redefine certain steps of an algorithm without changing its structure .
Main solution : Some methods are common , I've rewritten this method in every subclass .
When to use : There are some general methods .
How to solve : Abstract these general algorithms .
Key code : Implemented in abstract class , Other steps are implemented in subclasses .
Application example :1、 When building a house , The foundation 、 Routing 、 The pipes are all the same , It was only later in the building that there were differences like closets and fences . 2、 In the journey to the west, the Bodhisattva decided 81 difficult , This is a top-level logical skeleton . 3、spring Chinese vs Hibernate Support for , Encapsulate some of the established methods , For example, opening a transaction 、 obtain Session、 close Session etc. , Programmers do not repeat code that is already well written , Just drop an entity and you can save it .
advantage :1、 Encapsulate the invariant part , Extend the variable part . 2、 Extract public code , Easy to maintain . 3、 Behavior is controlled by the parent class , Subclass implementation .
shortcoming : Each different implementation needs a subclass to implement it , Causes the number of classes to increase , Make the system bigger .
Use scenarios :1、 There are methods common to many subclasses , And the logic is the same . 2、 Important 、 Complex methods , Consider as a template method .
matters needing attention : To prevent malicious operation , General template methods add final key word .
Realization
We will create a... That defines the operation Game abstract class , among , The template method is set to final, So it won't be rewritten .Cricket and Football It's expanded Game The entity class , They override methods of abstract classes .
TemplatePatternDemo, Our demo class uses Game To demonstrate the use of template patterns .
step 1
Create an abstract class , Its template method is set to final.
Game.java
public abstract class Game {
abstract void initialize();
abstract void startPlay();
abstract void endPlay();
// Templates
public final void play() {
// Initialize the game
initialize();
// Start the game
startPlay();
// End the game
endPlay();
}
}
step 2
Create entity classes that extend the above classes .
Cricket.java
public class Cricket extends Game {
@Override
void endPlay() {
System.out.println("Cricket Game Finished!");
}
@Override
void initialize() {
System.out.println("Cricket Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Cricket Game Started. Enjoy the game!");
}
}
Football.java
public class Football extends Game {
@Override
void endPlay() {
System.out.println("Football Game Finished!");
}
@Override
void initialize() {
System.out.println("Football Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Football Game Started. Enjoy the game!");
}
}
step 3
Use Game Template method play() To demonstrate how games are defined .
TemplatePatternDemo.java
public class TemplatePatternDemo {
public static void main(String[] args) {
Game game = new Cricket();
game.play();
System.out.println();
game = new Football();
game.play();
}
}
step 4
Verify output .
Cricket Game Initialized! Start playing.
Cricket Game Started. Enjoy the game!
Cricket Game Finished!
Football Game Initialized! Start playing.
Football Game Started. Enjoy the game!
Football Game Finished!
版权声明
本文为[Look, water is water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626122216.html
边栏推荐
- Considerations for importing idea package and calling its methods
- Learning C language diary from scratch -- day27 minesweeping
- TDD开发模式与DDD开发模式
- 常见的测试方式
- Swagger UI简介
- mysql rank() over,dense_ rank() over,row_ The difference between number () and over
- 物联网测试都有哪些挑战,软件检测机构如何保证质量
- Nexus私服——(三) 在项目实战中使用私服仓库
- Mongodb experiment -- data backup and recovery and database optimization
- The eleventh job of MySQL database - Application of view
猜你喜欢

Junit Introduction et Introduction

Nexus private server - (II) console installation of version 3.2.0, initial password location

Feign calls the service, and the called service Seata transaction is not opened or XID is empty

Paper reading - access pattern disclosure on searchable encryption: allocation, attack and deviation (2012)

A collection of common methods of data exploratory analysis (EDA)

【Redis笔记】数据结构和对象:字典

Punch in: 4.20 C language chapter - (1) first acquaintance with C language - (10) keywords typedef, static

Pytorch builds a two-way LSTM to realize time series forecasting (load forecasting)

Supporting early and scalable discovery of information websites

Measuring the impact of a successful DDoS attack on the customer behavior of managed DNS servi
随机推荐
数据库(二)MySQL表的增删改查(基础)
Chapter 3 basic SQL syntax
JUnit common notes
Full version operation process Jetson installs the latest opencv
[Reading Notes - > statistics] 07-01 introduction to the concept of discrete probability distribution geometric distribution
One way to disable Google cross domain
Junit Introduction et Introduction
Introduction to JUnit
Write down the daily temperature
Junit簡介與入門
Supporting early and scalable discovery of information websites
萌新看过来 | WeDataSphere 开源社区志愿者招募
Common test methods
Chapter I overview of database
How to modify the IP address of the rancher server
After the MySQL database runs the code, the question mark is displayed in Chinese?
Database 13th job transaction management
Nodejs link redis
Jackson
Vs2019 official free print control