I understand the design pattern (C ++ implementation) -- template method pattern)

Source: Internet
Author: User
Overview:

We have recently developed an application that supports extracting multiple types of compressed files and making them into PDF files. For our architecture, we need to support multiple types of compressed files, but there is a fixed operation order (extract the files first, read the files in it for analysis, and create PDF files ). We extract what they have in common: these operations are in a fixed order and put them in our parent class. Their change points: these specific operations are left to different subclasses for implementation. This is the template method mode, which defines the skeleton of an algorithm in an operation (fixed operation sequence in the example ), some steps are deferred to the subclass (in this example, multiple compressed files are decompressed ).

The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. It is a simple design mode, but it is a basic technology for code reuse. It is particularly important in class libraries. It is also widely used.

Class Diagram and instance:

Two roles are involved:

Abstractclass )Role:

Defines one or more abstract operations to implement sub-classes. These abstract operations are called basic operations, which constitute a step of top-level logic.

Defines and implements a template method. This template method is generally a specific method, which provides a skeleton of top-level logic, and the logical composition steps are postponed to subclass implementation in the corresponding abstract operations. The top-level logic may also call some specific methods. This defines our fixed operation sequence.

Specific template (concreteclass )Role:

Implement one or more abstract methods defined by the parent class. They constitute a step of top-level logic.

Each abstract template role can have any number of specific template roles, and each specific template role can provide these abstract methods (that is, the composition steps of top-level Logic) so that the implementation of the top-level logic is different. It is the support for different decompression of multiple compressed files.

Instance:

# Include <iostream> template <typename T> class caffeinebeverage // caffeine beverage {public: void preparerecipe () // caffeine beverage brewing method {boilwater (); // boil the water brew (); // brew pourincup (); // pour the caffeine beverage into the cup addcondiments (); // Add the seasoning} void boilwater () {STD :: cout <"boil water" <STD: Endl;} void BREW () {static_cast <t *> (this)-> BREW ();} void pourincup () {STD: cout <"pour coffee into Cup" <STD: Endl;} void addcondiments () {static_cast <t *> (this) -> addcondiments () ;}}; class coffee: Public caffeinebeverage <coffee >{ public: void BREW () {STD :: cout <"brewed coffee with boiling water" <STD: Endl;} void addcondiments () {STD: cout <"sugar and milk" <STD :: endl ;}; class tea: Public caffeinebeverage <tea >{public: void BREW () {STD: cout <"soaking tea with boiling water" <STD :: endl;} void addcondiments () {STD: cout <"add Lemon" <STD: Endl ;}; int main (void) {STD :: cout <"Coffee:" <STD: Endl; coffee C; C. preparerecipe (); STD: cout <STD: Endl; STD: cout <"mug Tea:" <STD: Endl; tea t; T. preparerecipe (); Return 0 ;}

Applicability:

1. Implement the unchanged part of an algorithm at one time, and leave the variable behavior to the subclass for implementation.

2. Common behaviors in each subclass should be extracted and concentrated into a common parent class to avoid code duplication. This is a good example of how Opdyke and Johnson describe "repartitioning to generalization. First, identify the differences in the existing code and separate the differences into new operations. Finally, replace these different codes with a template method that calls these new operations.

3. Control subclass extension. The template method only calls the "Hook" operation at specific points, so that the extension can only be performed at these points.

Implementation points:

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 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.

2. In addition to flexible changes to sub-steps, the reverse control structure "don't call me, let me call you" is a typical application of template method.

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


Lcl_data was originally created in csdn. Net [http://blog.csdn.net/lcl_data/article/details/95o61]

For other design patterns, see:Design patterns I understand

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.