Template Method template method mode

Source: Internet
Author: User
Behavior mode:

Behavior patterns involvedAlgorithmAnd the assignment of responsibilities between objects. Shift your attention from the control flow to the contact information between objects.

Behavior patterns include:Behavior modeAndBehavior Object Mode.Behavior mode usageInheritance MechanismDistribute actions between classes;Behavior Object Mode usageObject combinationInstead of inheritance.

Template Method template method mode

Template Method:

The template method mode is a behavior mode.Behavior Type Mode. It mainly solves the problem that a task usually has a stable overall operation structure during the software build process, but there are many changing requirements for each sub-step, or it cannot be implemented at the same time as the overall structure of the task due to inherent reasons.

Gof "design patterns": defines the skeleton of an algorithm in an operation, and delays some steps into sub-classes. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

Structure of the template method mode

Define scenarios

The single view structure is hard to understand. Let's combineProgramFor analysis. Now I want to define a scenario: I think everyone has ever played the extreme speed (I like it very much ). There are many cars in the game, but they are similar in terms of operation. It is nothing more than startup, run, stop and so on. There may be differences between the actions of a car, such as manual and automatic, but the interfaces are the same. In other words, these actions are basically the same. In combination with the template method mode, the structure (the operation on the car) is stable in this program, but the change lies in the sub-steps (the specific implementation of the operation behavior ).

Program Implementation and Structure Analysis

First, we need an abstract class of a car (abstractclass in the structure diagram)

Public abstract class abstractcar

{

Protected abstract string startup ();

Protected abstract string run ();

Protected abstract string stop ();

 

Public void driveonther, theroad ()

{

Console. writeline (startup ());

Console. writeline (run ());

Console. writeline (stop ());

}

}

In this sectionCodeAbstract METHODS startup, run, and stop are called primitive operation (primitive operations). They are extension points in the subclass. For example, we need to compile a Bora implementation, after he inherits abstractcar, he can implement his own steps on these primitive operations.

The driveontheroad method in abstractcar is called the template method), Template method uses primitive operation to define an algorithm,Is relatively stable. (Primitive operation is redefined in the subclass ).

Then we need to implement a BORA car (concreteclass in the structure diagram)

Public class Bora:Abstractcar // inherit

{

Protected override string startup ()

{

Return "Bora is startup ";

}

 

Protected override string run ()

{

Return "Bora is running ";

}

 

Protected override string stop ()

{

Return "Bora is stoped ";

}

}

Next we will implement the customer Program

Class Program

{

Static void main (string [] ARGs)

{

Clientapp. Drive (New Bora ());

Console. Read ();

}

}

Public class clientapp

{

Public static void drive (abstractcar)

{

Car. driveontheroad ();

}

}

The running result is as follows:

Bora is startup

Bora is running

Bora is stoped

Key Points of Template Method

1. The template method mode is a very basic design mode and has a large number of applications in the object-oriented system. It uses the simplest mechanism (Polymorphism of virtual functionsIt provides flexible extension points for many application frameworks and is the basic implementation structure for code reuse.

2. Apart from flexible changes to sub-steps, the reverse control structure of "don't call me. Let me call you" is a typical application of template method. "Don't call me. Let me call you" refers to a parent class that calls a subclass operation, rather than the opposite.

3. In terms of implementation, the virtual method called by the template method can be implemented or not implemented (abstract or pure virtual ), however, we recommend that you set them to the protected method. There is no specific implementation method, which should be called Hook operation (Hook operation). It provides the default behavior, and subclass can be expanded as necessary.

4. Reduce primitive operation as much as possible, because the more operations need to be redefined, the longer the client program

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.