C ++ design mode: Template Method Pattern)

Source: Internet
Author: User

Definition: The template method mode defines an algorithm skeleton in a method, and delays some steps to the subclass. The template method allows the subclass to keep the algorithm structure unchanged, redefines some steps in the algorithm.

Hook definition: A hook is a method declared in an abstract class, but it only has null or default implementations. The existence of hooks allows subclass to hook different points of algorithms. If you do not need a hook, The subclass determines it.

Scenario: We have a coffee shop with a special recipe to make coffee and tea. Some methods in the preparation process are similar. The steps are as follows:

1. Boil the water

2. Soak in coffee or tea with hot water

3. pour the drink into the cup

4. Add appropriate spices to the drinks. The coffee requires sugar and milk, while the tea requires lemon. When adding a seasoning, we need to ask the customer if they need to add the seasoning. At this time, the hook will be used.

Class diagram:

 

The C ++ code is as follows:

# Include <iostream>
Using namespace STD;

Class caffeinebeverage
{
Public:
Void preparerecipe ();
PRIVATE:
Virtual void BREW () = 0;
Virtual void addcondiments () = 0;
Void boilwater ();
Void pourincup ();
Virtual bool customerwantscondiments (); // hook
};

Class coffee: Public caffeinebeverage
{
PRIVATE:
Void BREW ();
Void addcondiments ();
Bool customerwantscondiments ();
};

Class Tea: Public caffeinebeverage
{
PRIVATE:
Void BREW ();
Void addcondiments ();
Bool customerwantscondiments ();
};

Void caffeinebeverage: preparerecipe ()
{
Boilwater ();
BREW ();
Pourincup ();
If (customerwantscondiments ())
{
Addcondiments ();
}
}

Void caffeinebeverage: boilwater ()
{
Printf ("boil water \ n ");
}

Void caffeinebeverage: pourincup ()
{
Printf ("pour into the cup \ n ");
}

Bool caffeinebeverage: customerwantscondiments ()
{
Return true;
}

Void coffee: BREW ()
{
Printf ("using boiling water to brew coffee powder \ n ");
}

Void coffee: addcondiments ()
{
Printf ("sugar and milk \ n ");
}

Bool coffee: customerwantscondiments ()
{
Char zanswer;
Printf ("Do you need milk and sugar in your coffee :");
Fflush (stdin );
Zanswer = getchar ();
Zanswer = toupper (zanswer );
If ('y' = zanswer)
{
Return true;
}
Else if ('n' = zanswer)
{
Return false;
}
Else
{
Printf ("input error, no seasoning \ n by default ");
Return false;
}
}

Void Tea: BREW ()
{
Printf ("Tea \ n soaked in boiling water ");
}
Void Tea: addcondiments ()
{
Printf ("add Lemon \ n ");
}

Bool Tea: mermerwantscondiments ()
{
Char zanswer;
Printf ("Do you need lemons in your tea :");
Fflush (stdin );
Zanswer = getchar ();
Zanswer = toupper (zanswer );
If ('y' = zanswer)
{
Return true;
}
Else if ('n' = zanswer)
{
Return false;
}
Else
{
Printf ("input error, no seasoning \ n by default ");
Return false;
}
}

Int main ()
{
Coffee coffee;
Tea;
Coffee. preparerecipe ();
Tea. preparerecipe ();
Return 0;
}

Running result:

Boil water
Brewed coffee powder with boiling water
Pour in the cup
Whether milk and sugar are needed in your coffee: Y
Sugar and milk
Boil water
Tea leaves soaked in boiling water
Pour in the cup
Do you need lemons in your tea: N

 

Note: The template method mode is a common mode, which is everywhere. In practical applications, many template methods are implemented, and they do not necessarily look the same as the design mentioned in the book. This design is common because this pattern is awesome for creating a framework. The framework controls how to do things, and the framework users specify the details of each step in the framework algorithm.

 

Reference books: head first design model

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.