Resolving design patterns with code and UML diagrams-template method patterns

Source: Internet
Author: User

At that time, I didn't know that I was using a model, but I thought it was very good. Very powerful. Therefore, I am very impressed with my learning today.

 

This mode uses object-oriented polymorphism to abstract public methods and does not need to be implemented. Other methods are implemented independently according to the business logic.

Wait until the call is completed.

 

Today, only code is available.

[Cpp]
// TemplateMethod. cpp: defines the entry point of the console application.
//
//************************************** **********************************/
/* @ Filename TemplateMethod. cpp
@ Author wallwind
@ Createtime
@ Function template method mode
@ Email wochenglin@qq.com
*/
/*************************************** *********************************/
 
 
# Include "stdafx. h"
# Include <iostream>
 
Using namespace std;
 
 
Class FuncBase
{
Public:
FuncBase (){}
Virtual ~ FuncBase (){}
 
 
Virtual void doOneStep () = 0;
// Virtual void doTwoStep () = 0;
Virtual void doThreeStep () = 0;
 
 
Void process ()
{
DoOneStep ();
// DoTwoStep ();
 
If (m_hook)
{
DoThreeStep ();
}
}
 
Void setHook (bool hook)
{
M_hook = hook;
}
 
Private:
Bool m_hook;
};
 
Class FuncBussOne: public FuncBase
{
Public:
FuncBussOne (){}
~ FuncBussOne (){}
 
Virtual void doOneStep ()
{
Cout <"FuncBussOne: doOneStep" <endl;
}
 
// Virtual void doTowStep ()
//{
// Cout <"FuncBussOne: doTowStep" <endl;
//}
 
Virtual void doThreeStep ()
{
Cout <"FuncBussOne: doThreeStep" <endl;
}
 
};
 
 
Class FuncBussTwo: public FuncBase
{
Public:
FuncBussTwo (){}
Virtual ~ FuncBussTwo (){}
 
Virtual void doOneStep ()
{
Cout <"FuncBussTwo: doOneStep" <endl;
}
 
// Virtual void doTowStep ()
//{
// Cout <"FuncBussTwo: doTowStep" <endl;
//}
 
Virtual void doThreeStep ()
{
Cout <"FuncBussTwo: doThreeStep" <endl;
}
 
};
Int _ tmain (int argc, _ TCHAR * argv [])
{
 
FuncBase * f;
F = new FuncBussOne;
F-> setHook (true );
F-> process ();
Cout <"---------" <endl;
F = new FuncBussTwo;
F-> setHook (false );
F-> process ();
 
Delete f;
 
Return 0;
}

 

Running result

 

 

Process is unique to the parent class. Other methods can be designed based on specific classes, but the logical process remains unchanged.

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.