catalogue
1, Problems in soybean milk production
Three. Template method mode principle class diagram
4, Template method mode to solve the problem of soybean milk production
5, Hook method of template mode
6, Source code analysis of application of template method pattern in Spring framework
7, Notes and details of template method pattern
1, Problems in soybean milk production
Prepare the procedure for making soybean milk as follows:
(1) Process for making soybean milk material selection -- > adding ingredients -- > soaking -- > smashing in the soybean milk machine.
(2) Different flavors of soybean milk can be made by adding different ingredients.
(3) The steps of material selection, soaking and crushing in the soybean milk machine are the same for making each flavor of soybean milk.
(4) Please use the template method mode (Note: because the template method mode is relatively simple, it is easy to think of this scheme, so you can use it directly instead of using the traditional scheme to export the template method mode).
2, Basic introduction
(1) Template Method Pattern, also known as template pattern, defines the template of the method executing it in an abstract class. Its subclasses can override the method implementation as needed, but the call will be made in the way defined in the abstract class.
(2) In short, the template method pattern defines the skeleton of an algorithm in operation, and delays some steps to subclasses, so that subclasses can redefine some specific steps of an algorithm without changing the structure of an algorithm.
(3) This type of design pattern belongs to behavioral pattern.
Three. Template method mode principle class diagram
3.1 schematic diagram
three point two Description of schematic class diagram - i.e. (roles and responsibilities of template method pattern)
(1) AbstractClass abstract class, which implements the template method and defines the skeleton of the algorithm. Specific subclasses need to implement other abstract methods operationr2,3,4.
(2) ConcreteClass implements the abstract method operationr2,3,4 to complete the steps of characteristic subclasses in the algorithm.
4, Template method mode to solve the problem of soybean milk production
(1) Application example requirements
Prepare the procedure for making soybean milk as follows:
Process for making soybean milk material selection -- > adding ingredients -- > soaking -- > smashing in soybean milk machine
Different flavors of soybean milk can be made by adding different ingredients
The steps of material selection, soaking and smashing in the soybean milk machine are the same for making each flavor of soybean milk (red bean, peanut soybean milk...)
(2) Train of thought analysis and illustration (class diagram)
(3) Code implementation
package com.atguigu.template; /** * @author wjd * @date 2021 September 23, 2010 10:11 */ //Abstract class representing soybean milk public abstract class SoyaMilk { //Template methods, make, and template methods can be made final without subclasses covering them public final void make() { select(); addCondiments(); soak(); beat(); } //Material selection public void select() { System.out.println("Choose fresh soybeans"); } //Add different ingredients, abstract methods and subclasses public abstract void addCondiments(); //soak public void soak() { System.out.println("Soybeans and ingredients begin to soak, which takes 3 hours"); } public void beat() { System.out.println("Put soybeans and ingredients into the soybean milk machine and start beating soybean milk"); } }
package com.atguigu.template; /** * @author wjd * @date 2021 10:18, September 23 */ public class RedBeanSoyaMilk extends SoyaMilk { @Override public void addCondiments() { System.out.println("Add good red beans"); } }
package com.atguigu.template; /** * @author wjd * @date 2021 10:19, September 23 */ public class PeanutSoyaMilk extends SoyaMilk { @Override public void addCondiments() { System.out.println("Add good peanuts"); } }
package com.atguigu.template; /** * @author wjd * @date 2021 10:20, September 23 */ public class Client { public static void main(String[] args) { System.out.println("----Making red bean milk----"); SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk(); redBeanSoyaMilk.make(); System.out.println("----Making peanut soybean milk----"); SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk(); peanutSoyaMilk.make(); } }
5, Hook method of template mode
(1) In the parent class of the template method pattern, we can define a method that does nothing by default, and the subclass can override it according to the situation. This method is called "hook".
(2) For example, we also want to make pure soybean milk without adding any ingredients. Please use the hook method to transform the previous template method.
(3) Look at the teacher's code demonstration:
package com.atguigu.template.improve; /** * @author wjd * @date 2021 September 23, 2010 10:11 */ //Abstract class representing soybean milk public abstract class SoyaMilk { //Template methods, make, and template methods can be made final without subclasses covering them public final void make() { select(); if (customerWantCondiments()) { addCondiments(); } soak(); beat(); } //Material selection public void select() { System.out.println("Choose fresh soybeans"); } //Add different ingredients, abstract methods and subclasses public abstract void addCondiments(); //soak public void soak() { System.out.println("Soybeans and ingredients begin to soak, which takes 3 hours"); } public void beat() { System.out.println("Put soybeans and ingredients into the soybean milk machine and start beating soybean milk"); } //Hook method to determine whether ingredients need to be added public boolean customerWantCondiments() { return true; } }
package com.atguigu.template.improve; /** * @author wjd * @date 2021 10:41, September 23 */ public class PureSoyaMilk extends SoyaMilk { @Override public void addCondiments() { //Empty implementation } @Override public boolean customerWantCondiments() { return false; } }
package com.atguigu.template.improve; /** * @author wjd * @date 2021 10:20, September 23 */ public class Client { public static void main(String[] args) { System.out.println("----------------Making red bean milk----------------"); SoyaMilk redBeanSoyaMilk = new RedBeanSoyaMilk(); redBeanSoyaMilk.make(); System.out.println("----------------Making peanut soybean milk----------------"); SoyaMilk peanutSoyaMilk = new PeanutSoyaMilk(); peanutSoyaMilk.make(); System.out.println("----------------Making pure soybean milk----------------"); SoyaMilk pureSoyaMilk = new PureSoyaMilk(); pureSoyaMilk.make(); } }
6, Source code analysis of application of template method pattern in Spring framework
(1) Template method pattern applied during initialization of Spring IOC container
(2) Code analysis + Role Analysis + description class diagram
(3) Class diagram for source code (describing hierarchical relationship)
7, Notes and details of template method pattern
(1) The basic idea is that the algorithm only exists in one place, that is, in the parent class, which is easy to modify. When the algorithm needs to be modified, as long as the template method of the parent class or some steps that have been implemented, the child class will inherit these modifications.
(2) Maximize code reuse. The template methods of the parent class and some implemented steps will be inherited by the child class and used directly.
(3) The template method of the parent class ensures that the structure of the algorithm remains unchanged, and the subclass provides the implementation of some steps.
(4) The disadvantages of this model: each different implementation needs a subclass implementation, resulting in an increase in the number of classes and a larger system.
(5) General template methods are added with the final keyword to prevent subclasses from rewriting template methods.
(6) Usage scenario of template method mode: when a process is to be completed, it needs to execute a series of steps. These steps are basically the same, but their individual steps may be different in implementation. Template method mode is usually considered for processing.