Template method Pattern (template mode)

Source: Internet
Author: User
Tags abstract inheritance

Mode Motive:

1, template method mode is based on the inheritance of code reuse Basic Technology, template method pattern structure and usage is also one of the core object-oriented design. In the template method pattern, you can put the same code in the parent class and put different method implementations in different subclasses.

2, in the template method pattern, we need to prepare an abstract class, the partial logic is implemented in the form of concrete methods and concrete constructors, and then declare some abstract methods to let subclasses implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic, which is the intent of the template method pattern. The template method pattern embodies many important ideas of object-oriented, and is a mode with high frequency.

Mode Intent:

Template method Pattern: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses, and the template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. Template method is a kind of behavior-type pattern

UML diagram:

role:

AbstractClass: Abstract classes Define a set of basic operations, which can be concrete or abstract, and a template method is implemented in an abstract class that defines the skeleton of an algorithm.

Concreteclass: A subclass of an abstract class that implements the steps of an abstract basic operation defined in a parent class to complete a subclass-specific algorithm, or overrides a specific basic operation implemented in a parent class.

Pattern Analysis:

1, template method mode is a kind of behavior pattern, in its structure diagram only the inheritance relationship between classes, there is no object association relationship.

2, in the use of template method mode, requires the development of abstract classes and the development of specific sub-class between the designers to collaborate. A designer is responsible for giving the outline and skeleton of an algorithm, while others are responsible for the various logical steps of the algorithm. The methods for implementing these specific logical steps are called basic methods (Primitive method), and the methods that summarize these basic law methods are called template methods, and the name of the template method pattern comes from here.

3, Template method: A template method is defined in the abstract class, the basic operation method is combined to form a total algorithm or a total behavior method.

4, the basic method: The basic method is to implement the methods of each step of the algorithm, is a template method part.

Abstract method

Specific methods (concrete method)

Hook method: "Hook" method and empty method

Hook methods (Hook method)

public void Template ()

{

Open ();

Display ();

if (Isprint ())

    {

print ();

    }

}

Public booleanisprint ()

{

return true;

}

The abstract class code looks like this:

Public abstract class AbstractClass

{

Public void Templatemethod ()//Template Method

   {

PrimitiveOperation1 ();

PrimitiveOperation2 ();

PrimitiveOperation3 ();

   }

public void PrimitiveOperation1 ()//Basic Method-Concrete method

{

Implementation code

}

public abstract void PrimitiveOperation2 (); Basic Method-Abstract method

public void PrimitiveOperation3 ()//Basic Method-hook method

{

}

}

The typical specific subclass code is as follows:

public class Concreteclass Extendsabstractclass

{

public void PrimitiveOperation2 ()

{

Implementation code

}

public void PrimitiveOperation3 ()

{

Implementation code

}

}

5, in the template method mode, because of object-oriented polymorphism, the subclass object will overwrite the parent class object at run time, and the methods defined in the subclass will override the methods defined in the parent class, so the basic method of the specific subclass will override the basic method defined in the parent class at run time, and the hook method of the subclass will override the parent class's Hook method. Thus, the implementation of the parent class method can be constrained by the hook method implemented in the subclass to implement the subclass's reverse control of the behavior of the parent class.

Advantages and Disadvantages:

Advantages

1. The template method model formally defines the algorithm in a class, and its subclasses implement the processing of the details.

2, template method mode is a kind of code reuse basic technology.

3. The template method pattern leads to a reverse control structure that invokes the operation of its subclasses through a parent class, and adds new behavior through the extension of the subclass, conforming to the "open and closed principle".

Disadvantages:

Each different implementation needs to define a subclass, which causes the number of classes to increase, the system is larger, the design is more abstract, but more in line with the "single responsibility Principle", so that the cohesion of the class can be improved.

scope of Use:

1, one-time implementation of an invariant part of the algorithm, and the variable behavior left to the subclass to achieve.

2. The public behavior of each subclass should be extracted and centralized into a common parent class to avoid code duplication.

3, some complex algorithms are segmented, the fixed part of its algorithm is designed as template method and the parent class concrete method, and some can change the detail by its subclass to realize.

4, control sub-class extension

mode extension:

1. Discussion on Succession

The template method pattern encourages us to use inheritance appropriately, and this pattern can be used to rewrite some related classes that have the same functionality, move the reusable generic behavior code into the parent class, and move the specialized behavior code into the subclass. This further illustrates that although there are some problems with inheritance reuse, it can be convenient for developers in some cases, and the template method pattern is one of the patterns that embody the advantages of inheritance.

2. Hollywood Principles

In the template method pattern, the subclass does not explicitly invoke the method of the parent class, but instead implements some specific business logic by overriding the parent class's method, which is called the Hollywood Principle (Hollywood Principle), and the Hollywood principle is defined as: "Don't call us, We will give you a call (Don ' t phone us, we'll be calling you).

In the template method pattern, the Hollywood principle is that the subclass does not need to call the parent class, and the child class is called by the parent class, and the implementation of some steps is written in the subclass, and the parent class controls the entire process.

3, the use of hook method

The introduction of the hook method allows subclasses to control the behavior of the parent class.

The simplest hook method is an empty method, or you can define a default implementation in the Hook method, and if the subclass does not overwrite the hook method, the default implementation code of the parent class is executed.

A more complex hook method can constrain other methods, which typically return a Boolean type, which returns TRUE or false, to determine whether to perform a basic method.

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.