Template method Mode

Source: Internet
Author: User

Template Method Pattern: defines the skeleton of an algorithm in a method, and delays some steps into subclasses. The template method allows subclasses to redefine some of the steps in the algorithm without altering the algorithm structure.

Hollywood principles: Low-level components must never invoke a high-layer component, and high-level components control when and how to involve lower-layer components. The Hollywood principles teach us a trick to create a resilient design.

#ifndef Caffeinebeverage_h#defineCaffeinebeverage_h#include<iostream>using namespacestd;classcaffeinebeverage{ Public:    voidPreparerecipe () {boilwater ();        Brew ();        Pourincup (); if(Customerwantscondiments ()) {addcondiments (); }            }    Virtual voidBrew () =0; Virtual voidAddcondiments () =0; voidBoilwater () {cout<<"Boiling Water"<<Endl; }    voidPourincup () {cout<<"pouring into Cup"<<Endl; }    //You can use policy mode optimizations for hooks.     Virtual BOOLcustomerwantscondiments () {return true; }};#endif
Caffeinebeverage
#ifndef tea_h#defineTea_h#include"CaffeineBeverage.h"#include<iostream>using namespacestd;classTea: Publiccaffeinebeverage{ Public:    voidBrew () {cout<<"steeping the tea"<<Endl; }    voidaddcondiments () {cout<<"Adding Lemon"<<Endl; }};#endif
Tea
#ifndef Coffee_h#defineCoffee_h#include"CaffeineBeverage.h"#include<iostream>#include<string>using namespacestd;classCoffee: Publiccaffeinebeverage{ Public:    voidBrew () {cout<<"Dripping Coffee through filter"<<Endl; }    voidaddcondiments () {cout<<"Adding Sugar and Milk"<<Endl; }    BOOLcustomerwantscondiments () {stringAnswer =string(""); cout<<"would milk and sugar with your coffee (y/n)?"<<Endl; CIN>>answer; if(Answer = =string("y"))        {            return true; }        Else        {            return false; }    }};#endif
Coffee
#include"CaffeineBeverage.h"#include"Coffee.h"#include"Tea.h"intMain () {Tea* Tea =NewTea; Tea-Preparerecipe (); Coffee* Coffee =NewCoffee; Coffee-Preparerecipe (); return 0;}
main.cpp

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.