Design Mode note (14)-template method mode (behavior type)

Source: Internet
Author: User
Document directory
  • Gof Definition
  • Motivation
  • Key points of the template method mode
Gof Definition

Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

Motivation

In the process of software building, a task often has a stable overall operation structure, but each sub-step has a lot of changing requirements, or because of inherent reasons (such as the relationship between the framework and the application), it cannot be implemented simultaneously with the overall structure of the task. On the premise of determining the stable operation structure, how can we flexibly cope with the changing or late implementation needs of each sub-step?

Template Method pattern structure:

Abstractclass class: This abstract class defines a template method templatemethod. This method is usually a specific method, which combines some abstract operations in the abstract class and delays implementation to the subclass.

Concreteclass class: one or more abstract methods defined by the parent class as required.

A small example is used to reflect the above structure:

/// <Summary> // test the vehicle class ---> abstractclass // </Summary> public abstract class vehical {protected abstract void start (); // primitiveoperation protected abstract void run (); // primitiveoperation protected abstract void trun (); // primitiveoperation protected abstract void stop (); // primitiveoperation /// <summary> /// equivalent to templatemethod /// </Summary> Public void test () {// start () for recording and other operations (); run (); trun (); stop () ;}/// <summary >/// Red Flag ---> concreteclass // </Summary> public class hongqicar: vehical {protected override void start () {console. writeline ("test the Red Flag startup");} protected override void run () {console. writeline ("test Red Flag driving");} protected override void trun (INT degree) {console. writeline ("test Red Flag steering");} protected override void stop () {console. writeline ("test the Red Flag stop"); }}/// <summary> // QQ car ---> concreteclass // </Summary> public class qqcar: vehical {protected override void start () {console. writeline ("test QQ car startup");} protected override void run () {console. writeline ("test QQ car driving");} protected override void trun (INT degree) {console. writeline ("test QQ car steering");} protected override void stop () {console. writeline ("test QQ car stop") ;}/// <summary> // Customer Code /// </Summary> public class app {static void main () {vehical; vehical = new hongqicar (); vehical. test (); vehical = new qqcar (); vehical. test (); console. readline ();}}
Key points of the template method mode

The template method mode is a basic design mode and has a large number of applications in the object-oriented system. It uses the most concise mechanism (the polymorphism of virtual functions) to provide flexible extension points for many application frameworks and is the basic implementation structure of code reuse.

In addition to 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.

In terms of implementation, the Virtual Methods called by the template method can be implemented, or there can be no implementation (Abstract METHODS, pure virtual methods), but it is generally recommended to set them as protected methods.

Return to the beginning (INDEX)

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.