background
Many times we always follow the rules and repeat the same thing, such as now if I do not insist on continuing to study, it must be like a walking dead and repeat the same logic code, hehe, every time after the completion of the code will be a little bit, the world only I can do this excellent work, do not say, I used to be a little picky and self-righteous. However, now that I realize that the programmer needs to learn, I will change the net name to learn more, which means to urge me to keep learning.
So, how should we avoid this boilerplate repetition? That is to extract the duplicated code, customize the way to implement the change!
1. Intention to use
Code reuse, logic does not change!
2. Life Example
Each time you enter the company door, the door will open automatically, and every time you leave the door of the company, the door will be closed automatically, this open and closed process should be done manually by everyone, but in order to avoid some people will not open the door, or too much trouble to open the door, so the automatic door is created! This small process implies a design pattern-the template method pattern
3. Java Example (framework, JDK, JEE)
JDBC, LDAP query or operation, there are always some duplicate code, It's estimated that anyone who has studied Java knows that
First, if the driver class is not loaded then load a driver;
Second, Connection con = drivermanger.getconnection gets a corresponding data source connection;
Then, using connection con to get the corresponding precompiled statement object PreparedStatement, I don't know if the class name was written incorrectly.
Next, custom SQL, processing SQL statement parameters, placeholder substitution, etc., make it a statement that can be run
Continue, if the database operation is naturally open transaction, there is no need to open transaction waste resources
Finally, perform the operation, close the transaction, close the statement, close the connection
see no, the next time you want to execute another SQL to step by step, the bad taste of the code tells us that repetition is very error-prone, so how to avoid this duplication of writing? after spring has encapsulated JDBC, it's good to avoid duplication , and when using spring, just get the transaction, open the transaction, write our business logic inside the transaction, and the other operations are done automatically in the template style.
I also use Lucene to write a database table index creation method, because the database records more, the first thought is definitely paged query, or a breath query estimated memory overflow, since it is paging, then each page after the data to be processed immediately, in order not to affect performance, So the entire query is placed in a data connection, then, is the data connection and paging is a template, and how these query results to operate is a custom extension of the place, so the operation method is written as an abstract method for the client implementation.
4. Pattern class Diagram
Parent Role : Defines an abstract class that first declares some method names that complete a particular task (its implementation is given to subclasses to complete), which are called steps. and define a method, the implementation of this method is to invoke the previous methods of the Declaration, the key is that the order of these methods have been determined here (the subclass cannot be changed), called the template method.
Subclass Role : Implements the parent class role for those abstract methods (steps), and inherits its template methods. So regardless of the implementation step, its template is not changed.
Summarize:
The template method is actually a step to accomplish something, and these steps have been defined in the method of the parent class, the steps need to customize the method of abstraction out to the subclass to implement.
5. Advantages of the Model
Template Method pattern: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
The template method pattern is to show his advantage by moving the invariant behavior to the superclass and removing the duplicated code in the subclass.
The template method pattern provides a good code reuse platform
When immutable behavior and mutable behavior are mixed together in a subclass implementation of a method, the invariant behavior recurs in the subclass. By using the template method to move these behaviors to a single place, it helps the subclass to get rid of the repetitive and unchanging behavior.
through the above definition, we do not have to use subclasses to implement the parent class, but we can define the algorithm rule step in the parent class to change the abstract method of the parent class to invoke an abstract class of abstract method, and this method concrete implementation is given to the subclass of this abstract class to complete, so that, Sometimes it is good to separate the axes of certain classes.
6. Comparison with similar models
This design pattern, use very much, also not easy to produce misunderstanding, so there is nothing to compare
The design pattern of the vegetable--template method pattern