14. template method (behavior mode)

Source: Internet
Author: User

Ubiquitous Template Method
If you only want to master a design mode, it is the template method!

Change is the eternal subject of software design. How can we manage the complexity brought about by changes? The artistic and complexity of the design pattern lies in how to analyze, discover changes and stability points in the system, and use specific design methods to deal with such changes.

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?

Intent)
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.
-- Design Pattern gof

Structure)

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace template_method
{
// Framework developer-development first

Public abstract class vehical // indicates a vehicle
{
Protected abstract void startup (); // Add protected
Protected abstract void run ();
Protected abstract void turn (INT degree );
Protected abstract void stop ();
Public void test () // Add public before non-virtual Method
{
// Test data record
Startup (); // late binding-left to application developers; Extension point

// Test data record
Run (); // late binding-left to application developers; Extension point

// Test data record
Turn (40); // late binding-left to application developers; Extension point

// Test data record
Stop (); // late binding-left to application developers; Extension point

// Generate a test report
}

}


// Application development-Late Development

Public class hongqicar: vehical
{
Protected override void startup ()
{
Console. writeline ("Startup ");
}

Protected override void run ()
{
Console. writeline ("Run ");
}
Protected override void turn (INT degree)
{
Console. writeline ("degree ");
}
Protected override void stop ()
{
Console. writeline ("stop ");
}

}

Class vehicaltestframework
{
Public static void dotest (vehical)
{
Vehical. Test ();
}
}
Class Program
{
Static void main (string [] ARGs)
{
Vehicaltestframework. dotest (New hongqicar ());
Console. Readline ();
}
}
}

 

Key points of the template method mode
• The template method mode is a very basic design mode and has a large number of applications in object-oriented systems. 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 not implemented (Abstract methods and pure virtual methods ), however, we recommend that you set them to the protected method.

The template method mode is actually one of the most common modes in all modes, and many may have used the template method mode without realizing that they have already used this mode. The template method mode is a basic technology based on inherited code reuse. The structure and usage of the template method mode is also the core of object-oriented design.

 

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.