preface
The template approach pattern is a behavioral design pattern with simple principles and code implementation.
directory
A, definitions,
Define the framework of an algorithm in an operation, deferring some steps to subclasses so that subclasses can redefine specific steps of an algorithm without changing the structure of the algorithm.
The subclasses below a parent class use the general logic by inheriting from the parent class and optimizing some of the steps according to their own needs
Two, mode principle analysis
As an example of the hook education, design a simple continuous integration release system where the R & D department develops code on GitLab and uses a fixed release process to launch the program.
Public final void buildFlow() {pullCodeFromGitlab(); // Pull the code compileAndPackage() from GitLab; // Compile and package copyToTestServer(); // Deploy testing(); / / test copyToRemoteServer (); // Upload the package to the online environment startApp(); } public abstract void pullCodeFromGitlab(); public abstract void compileAndPackage(); public abstract void copyToTestServer(); public abstract void testing(); Private void copyToRemoteServer() {system.out.println (" automatically upload the start App package to the corresponding online server "); } private void startApp() {system.out.println (" automatically start online App"); }}Copy the code
Implement two subclasses respectively: 1, to achieve the local package compilation and upload; 2. Fully automated continuous integration releases
public class LocalDeployFlow extends DeployFlow{ @Override public void pullCodeFromGitlab() { System.out.println(" Manually pull code to local computer......") ); } @override public void compileAndPackage() {system.out.println (" manually compile package on local computer......") ); } @override public void copyToTestServer() {system.out.println (" upload package to local test service via SSH......") ); } @override public void testing() {system.out.println (" run a manual test......") ); } } public class CicdDeployFlow extends DeployFlow{ @Override public void pullCodeFromGitlab() { System.out.println(" Continuous integration server pulls code to node server......") ); } @override public void compileAndPackage() {system.out.println (" automatically compile & package......") ); } @override public void copyToTestServer() {system.out.println (" automatically copy packages to the test environment server......") ); } @override public void testing() {system.out.println (" run automated tests......") ); }}Copy the code
Run a unit test
Public class Client {public static void main(String[] args) {system.out.println (" start the local manual publishing process ======"); DeployFlow localDeployFlow = new LocalDeployFlow(); localDeployFlow.buildFlow(); System.out.println("********************"); System.out.println(" start CICD publishing process ======"); DeployFlow cicdDeployFlow = new CicdDeployFlow(); cicdDeployFlow.buildFlow(); }} // The output starts the local manual publishing process ====== Manually pull the code to the local computer...... Manually compile and package...... on your local computer Manually upload the package to the local test service...... using SSH Perform manual tests...... Unified automatic upload to start the App server package to the corresponding line Unified start automatically online App * * * * * * * * * * * * * * * * * * * * start CICD release process = = = = = = continuous integration server code will pull to the node on the server... Compile & package automatically...... Automatically copy packages to the test environment server...... Run automated tests...... Automatically upload and start App packages to corresponding online servers and automatically start online apps in a unified mannerCopy the code
From the analysis of the above scenario, we can see that the biggest feature of the template method pattern application scenario is that the algorithm is usually optimized for specific steps, rather than the entire algorithm is modified. Once the overall algorithm framework has been defined, subclasses cannot be modified directly because their usage scenarios are directly affected by the superclass scenarios.
Iii. Usage Scenarios
- Multiple subclasses have public methods and the logic is basically the same
- For important and complex algorithms, the core algorithm can be designed as a template method, and the peripheral details can be implemented by each subclass
- When refactoring, the template approach is a frequently used pattern that extracts the same code into a parent class and then constrains its behavior through “hook functions.
Four advantages,
- Effectively eliminates duplicate code
- Encapsulate invariant parts and extend variable parts
- Extract common code for easy maintenance
- Behavior is controlled by the parent class and implemented by the child class
Five, the disadvantages
-
Inconsistent with the open closed principle. A parent class calls a child class to implement the operation, through the child class extension to add new behavior, but the result of the child class execution will be affected by the parent class, does not comply with the open closed principle of “closed to modify”.
-
Makes the code harder to read. Because some steps or methods of the parent class are delayed to the subclass, the code logic needs to jump to different subclasses to read. If the number of subclasses is large, the jump will be too much, and it is not convenient to contact the context logic clues.
-
Violation of the Richter substitution principle. While a parent class in the template method pattern provides a generic implementation, an action deferred to a subclass becomes some kind of custom action that, if replaced, may cause the parent class to become unavailable or the overall logic to change.