Design Mode-template method mode
Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. template Method let subclasses redefine certain steps of an algorithm without changing the algorithm's structure. defines the framework of an algorithm in an operation, and delays some steps to the subclass. This allows the subclass to redefine some specific steps of the Algorithm without changing the structure of an algorithm.
Two Roles in the template method mode:
Abstract Template: this role defines one or more Abstract operations for sub-classes to implement. These Abstract operations are basic operations, one or more template methods need to be defined. Generally, the specific method is used to define a framework.
A specific Template role (Concrete Template): implements one or more abstract methods defined in an abstract Template. Each abstract Template can have multiple Specific templates, each specific template role can provide different implementations of these abstract methods.
Class diagram of the template method mode:
Various implementation codes:
Abstract template class:
Package com. zz. template;/*** abstract template * Copyright May 15, 2015 * created by txxs * all right reserved */public abstract class AbstractClass {// basic method protected abstract void operation (); // template method public void templateMethod () {// call the basic method to complete the related logic this. operation ();}}TEMPLATE 1:
Package com. zz. template; /*** specific template role 1 * Copyright May 15, 2015 * created by txxs * all right reserved */public class ConcreteClass1 extends AbstractClass {// basic implementation method @ Overrideprotected void operation () {// business logic processing }}
Template 2:
Package com. zz. template;/*** specific template class 2 * Copyright May 15, 2015 * created by txxs * all right reserved */public class ConcreteClass2 extends AbstractClass {// implement basic business method @ Overrideprotected void operation () {// business logic processing }}
Client:
Package com. zz. template;/*** test class * Copyright May 15, 2015 * created by txxs * all right reserved */public class Client {public static void main (String [] args) {AbstractClass class1 = new ConcreteClass1 (); AbstractClass class2 = new ConcreteClass2 (); // call the template method class1.templateMethod (); class2.templateMethod ();}}
Advantages of the template method mode:
1. encapsulate the unchanged part and expand the variable part. The unchanged part can be encapsulated into the parent class, and the variable part can be extended through inheritance.
2. Extracting public code is conducive to maintenance. Put the public code in the parent class. during maintenance, you only need to modify the parent class code.
3. The behavior is controlled by the parent class and implemented by sub-classes. The basic methods in the template method mode are implemented by sub-classes. Therefore, sub-classes can add corresponding functions through extensions to comply with the open and closed principles.
Use Cases of the template method:
1. Multiple child classes have common methods, and the logic is basically the same.
2. You can design important, complex, and Core algorithms as template methods. The details of these algorithms are implemented by sub-classes.
3. During refactoring, the same code can be extracted to the parent class.