[C + + design mode]template Template method mode

Source: Internet
Author: User

Template mode: Defines an algorithm skeleton in an operation, and delays some steps into subclasses.

According to the example of "Headfirst design mode", the algorithm Framework (flow) for brewing and brewing coffee is the same, but some algorithms are implemented differently and some are the same.
We can encapsulate the common algorithm framework as a virtual base class, declaring the same algorithm as non-overriding (static), and different algorithms declared as pure virtual functions to be implemented by subclasses.
You can use the hook () function to handle small differences in algorithmic frameworks.

When you see this, you may think of strategy mode. The strategy pattern is also the difference between the algorithms that can be changed and the algorithms that are not easily changed, but the most fundamental difference between the policy pattern and the template method pattern is:
The policy pattern is the class combination, the invariant algorithm remains in the original class, but the algorithm will be overloaded separately encapsulated as a virtual base class, subclasses implement their own version, so the original class is
Different interface subclasses can be combined to invoke different algorithms.
Template method pattern is class inheritance, the algorithm framework (STEP) is encapsulated as a virtual base class, and the algorithm framework is not covered, subclasses can only have different implementations of individual steps. The base class can also introduce the hook () function to fine-tune the algorithm framework. Hooks () hook function is very simple, the base class hook () function can be defined as NULL, you can also define some operations, subclasses can be overloaded with the hook () function of the base class.


Here is the template method mode without hook () hooks:

Class Caffeinebeverage  //Caffeine Drink {public:void preparerecipe ()///Caffeine drink brewing method {  boilwater ();  Boil water to  Brew ();    Brewing  Pourincup ();  Pour the caffeine drink into the cup  addcondiments ();//Add seasoning} void Boilwater () {std::cout << "boil water" << std::endl;} virtual void Br EW () = 0; void Pourincup () {std::cout << pour coffee into cup << Std::endl;} virtual void addcondiments () = 0;}; Class Coffee:public Caffeinebeverage{public:void Brew () {std::cout << brewed coffee with boiling water << Std::endl;} void Addcond Iments () {std::cout << "add sugar and milk" << Std::endl;}}; Class Tea:public Caffeinebeverage{public:void Brew () {std::cout << soak tea with boiling water << Std::endl;} void Addcondime NTS () {std::cout << "add lemon" << Std::endl;}}; int main (void) {std::cout << "Cup coffee:" << Std::endl; Coffee C; C.preparerecipe (); Std::cout << Std::endl; Std::cout << "cup tea:" << Std::endl; Tea T; T.preparerecipe (); return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[C + + design mode]template Template method mode

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.