Behavioral mode: template Method Mode
1. Intention
Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. TemplateMethod allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
2. Alias
None
3. Motivation
Consider an Application framework that provides Application and Document classes. The Application class is responsible for opening an existing document stored in an external form, such as a file. Once the information in a Document is duplicated in the file, it is represented by a Document object.
4. Applicability
The following uses the Strategy Mode:
-Implement the unchanged part of an algorithm at one time and leave the variable behavior to the subclass for implementation.
-Common behaviors in each subclass should be extracted and centralized into a common parent class to avoid code duplication.
-Control subclass expansion. The template method is designed to call the "hook" operation at specific points so that expansion can only be performed at these points.
5. Structure
To explain the template method mode, it means that an abstract class contains a main method, and then defines 1... N methods can be abstract or actual methods. Define a class, inherit the abstract class, override the abstract method, and call the abstract class to call the subclass, the diagram is as follows:
Is it in AbstractCalculatZ notebook? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> authorization + 0z8LD5rXEwP3X06O6PC9wPg0KPGgyIGlkPQ =" 6 sample code ">6. Sample Code
Implementation Code:
Auxiliary class:
Public abstract class AbstractCalculator {/* main method to call other methods of this class */public final int calculate (String exp, String opt) {int array [] = split (exp, opt); return calculate (array [0], array [1]);}/* quilt class override method */abstract public int calculate (int num1, int num2 ); public int [] split (String exp, String opt) {String array [] = exp. split (opt); int arrayInt [] = new int [2]; arrayInt [0] = Integer. parseInt (array [0]); arrayInt [1] = Integer. parseInt (array [1]); return arrayInt ;}}
public class Plus extends AbstractCalculator { @Override public int calculate(int num1,int num2) { return num1 + num2; }}
Test class:
public class StrategyTest { public static void main(String[] args) { String exp = 8+8; AbstractCalculator cal = new Plus(); int result = cal.calculate(exp, \+); System.out.println(result); }}
Trace the execution process of this applet: first, set exp and "+" as parameters and call the calculate (String, String) method in the AbstractCalculator class. In the calculate (String, String) and then call the calculate (int, int) method. After the return num1 + num2 method is executed, return the value to the AbstractCalculator class, assign it to the result, and print it out. It just verifies our initial thinking.
7. Related Modes
Factory Method mode, which is often called by template methods.
Strategy: The template method uses inheritance to change part of the algorithm, and Strategy uses delegation to change the entire algorithm.