Behavioral mode: template Method Mode

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.