Template method Mode (Template methods pattern)

Source: Internet
Author: User
Tags abstract inheritance

Mode motivation:

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

2. In the template method pattern, we need to prepare an abstract class, implement part of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to enable the subclass to 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 intention of the template method pattern. Template method pattern embodies many important ideas of object-oriented, and is a mode with high frequency.

Mode Intent:

Template method Patterns: Defining the skeleton of an algorithm in an operation, and delaying some steps into subclasses, the template method allows subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm (Template). Template method is a kind of behavior pattern

UML diagram:

role:

AbstractClass: An abstract class defines a series of basic operations, which can be concrete or abstract, and implement a template method in an abstract class to define the skeleton of an algorithm.

Concreteclass: Concrete subclasses are subclasses of abstract classes that implement the steps of an abstract basic operation defined in a parent class to complete a subclass-specific algorithm, or you can override the specific basic operations that are implemented in the parent class.

Pattern Analysis:

1, the template method pattern is a class behavior pattern, in its structure diagram only has the inheritance relation between the class, does not have the object association relation.

2. In the use of template method patterns, it is required to develop an abstract class and to develop a specific subclass of collaboration between the designers. A designer is responsible for the outline and skeleton of an algorithm, while others are responsible for the logical steps of the algorithm. The methods for implementing these specific logical steps are called basic methods (primitive method), and the method of summarizing these basic methods is called the template method (Template methods), and the name of the template method pattern is from here on.

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

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

Abstraction methods (abstract method)

Specific methods (concrete method)

Hook method: "hooking" Method and Null method

Hook methods (Hooks 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 looks like this:

public class Concreteclass Extendsabstractclass

{

public void PrimitiveOperation2 ()

{

Implementation code

}

public void PrimitiveOperation3 ()

{

Implementation code

}

}

5. In Template method mode, because of the object-oriented polymorphism, the subclass object overwrites the parent object at run time. The method defined in the subclass also overrides the method defined in the parent class, so when the program is running, the basic method of the concrete subclass overrides the basic method defined in the parent class, and the child's Hook method overrides the parent's hook method. Thus, the implementation of the parent-class method can be constrained by the hook method implemented in the subclass to realize the inverse control of the subclass to the parent-class behavior.

pros and Cons:

Advantages

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

2. Template method pattern is a basic technique for code reuse.

3, template method mode leads to a reverse control structure, through a parent class call its subclass of operations, through the extension of the child class to add new behavior, in line with the "open-closing principle."

Disadvantages:

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

Use range:

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

2, the public behavior in 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 the algorithm is designed as template method and the parent class concrete method, and some can change the details by its subclasses to implement.

4, control the extension of the child class

schema extension:

1. Discussion on inheritance

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

2. Hollywood Principles

In Template method mode, subclasses do not explicitly invoke methods of the parent class, but instead implement some specific business logic by overwriting the parent class, which is called the Hollywood Principle (Hollywood principle), and the Hollywood principle is defined as: "Don't call us, We'll give you a call (Don T calls us, we'll called you).

In Template method mode, the Hollywood principle is embodied in the following: Subclasses do not need to invoke the parent class, and the subclass is invoked by the parent class, and the implementation of some steps is written in a 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 execute the default implementation code of the parent class if the subclass does not overwrite the hook method.

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

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.