Template Method (templatemethod)

Source: Internet
Author: User

First, let's talk about the importance and significance of the template method. In fact, many people should have used the template method,

They just don't realize it. For example, when you create a form or page, you use the template method,

Because the form inherits from the form and the page inherits from the page, here you can regard form and page as a template,

However, our operations in our own forms or pages only define our own specificAlgorithm.

The template mode is based on inheritance.CodeIt is also a simple design model,

It can also be said that the structure and usage of the template method mode is also the core of object-oriented design.

The following describes the template method mode definition.

Define the skeleton of an algorithm in an operation,

Deferring some steps to subclasses,

Lets subclasses redefine certain steps of on Algorithm without changing the algorithm's structure.

That is, to define the skeleton of an algorithm in an operation, and to delay some steps to the subclass, without changing the structure of the algorithm and redefining some of its steps.

Simply put, the template method is used to define an algorithm template, and some specific implementations of this template are placed in the subclass to complete.

There are abstract classes and specific classes in the template method mode,

Abstract classes give the outline and skeleton of an algorithm, while other classes give the logical steps of this algorithm,

The methods that represent these specific logical steps are called Basic methods, and the methods that aggregate these basic methods are the template methods.

Next, let's take a look at the structural class diagram of the template method mode.

From the above class diagram, we can see that a templatemethod, that is, the template method, is defined in the abstract class abstractclass,

In addition, the algorithm skeleton is defined, but the specific implementation of the algorithm is placed in concreteclass.

Note the differences between the template method and the policy mode,

The template method defines the skeleton of an algorithm by the abstract class, and then the subclass is used to implement the logic in the algorithm.

The policy mode encapsulates multiple actions (that is, algorithms) that can be replaced by each other, and then dynamically determines the behavior on the client.

For the difference, the structure class diagram of the Policy mode is shown below. Pay attention to the difference between the two.

Next we will introduce the Hollywood principles,

In Hollywood, when an actor submits his resume to a Hollywood entertainment company,

All we can do is wait. The Hollywood company will tell them: "Don't call us, we will call you ".

Obviously, Hollywood principles reflect the full control of Hollywood companies on the entire entertainment project,

The candidates only passively follow the arrangement and complete a specific step in the process as needed.

If we consider the above Hollywood principles in OO, we can rewrite it to avoid calling us and we will call you,

What are the benefits?

Through Hollywood principles, we allow the components at the underlying layer to interact with the system,

However, when or how to call these underlying components is determined by the high-level components.

That is, Senior components act as Hollywood companies, and actors act as underlying components,

Therefore, the principle of high-level components for the underlying components is: do not call me. I will call you as a high-level component to call the underlying components.

The Application of Hollywood principles in the template method mode is: the abstract class will tell its specific subclass: "Do not call us, we will call you ".

Next, let's take a look at the specific code of the template method mode.

Abstractclass code

namespace templatemethod
{< br> ///


// abstract class
// defines and implemented a templatemethod method
// and declared the logical composition steps in it
/// just defined as an abstract method
// to make these methods must be delayed to subclass implementation
///
Public abstract class abstractclass
{
///
// composition step of the Declaration logic
/// and define it as an abstract method
///
public abstract void primitiveoperation1 ();
public abstract void primitiveoperation2 ();

/// <Summary>
/// The basic skeleton of the algorithm is defined in this template method.
/// The basic skeleton is to call the basic method 1 first.
/// Call the basic method 2 again
/// </Summary>
Public voidTemplatemethod()
{
Primitiveoperation1 ();
Primitiveoperation2 ();
}
}
}

Next, let's take a look at the specific code of the two classes concreteclassa and concreteclassb.

Using system;

NamespaceTemplatemethod
{
Public classConcreteclassa: abstractclass
{
Public override voidPrimitiveoperation1()
{
Console. writeline ("Implementation of step 1 of the algorithm skeleton defined in the abstract class in Class ");
}

Public override voidPrimitiveoperation2()
{
Console. writeline ("Implementation of step 1 of the algorithm skeleton defined in the abstract class in Class ");
}
}
}

Using system;

NamespaceTemplatemethod
{
Public classConcreteclassb: abstractclass
{
Public override voidPrimitiveoperation1()
{
Console. writeline ("step 1 of the algorithm skeleton defined in the abstract class is implemented in Class B ");
}

Public override voidPrimitiveoperation2()
{
Console. writeline ("step 1 of the algorithm skeleton defined in the abstract class is implemented in Class B ");
}
}
}

Client code

Using system;

NamespaceTemplatemethodtest
{
Class Program
{
Static void main (string [] ARGs)
{
Templatemethod. abstractclassAbstractclass;
Abstractclass = new templatemethod. concreteclassa ();
Abstractclass. templatemethod();

Console. writeline ();

Abstractclass = new templatemethod. concreteclassb ();
Abstractclass. templatemethod ();

Console. Read ();
}
}
}

Effect

I used to see a good example of the template method pattern in "big talk Design Pattern,

Here is an example of an exam paper. Why is this example good,

Because you can think of the exam paper as a template method in the abstract class, but what about the answers written by every student,

The test paper is regarded as a template for the specific implementation of the algorithm logic, that is, a filling problem,

Therefore, every student only needs to fill in his own things in this template. Since it is an exam paper,

Fill in the answer, of course. In this case, each student needs to fill in the answer,

In this way, the code can be reused to the maximum extent.

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.