Design Mode C ++ study note 9 (Template Method mode)

Source: Internet
Author: User

The template mode is also quite simple and commonly used. The template mode defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. TemplateMethod allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. The example of cbf4life is the Hummer model. For more details, refer to the original author's blog: cbf4life.cnblogs.com. Here, we only propose the design and implementation methods of C ++ to Facilitate the use with demand.

9. 1. Explanation

Main (), customer

CHummerModel, Hummer Model

CHummerH1Model, Hummer Model 1

CHummerH2Model, Hummer Model 2

Note: The Start, Engineboom, Alarm, and Stop virtual functions are declared in CHummerModel and implemented by the derived classes. The Run of the base class organizes the logic and calls the functions implemented by these derived classes.

Note: The "Run" in the base class should not be overwritten by the derived class.

Check the Code:

// HummerModel. h

# Pragma once
Class CHummerModel
{
Public:
CHummerModel (void );
Virtual ~ CHummerModel (void );
Void Run ();
Protected:
Virtual void Start () = 0;
Virtual void Stop () = 0;
Virtual void Alarm () = 0;
Virtual void EngineBoom () = 0;
Virtual bool IsAlarm ();
};

// HummerModel. cpp

# Include "StdAfx. h"
# Include "HummerModel. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CHummerModel: CHummerModel (void)
{
}
CHummerModel ::~ CHummerModel (void)
{
}
Void CHummerModel: Run ()
{
// Start the car first
Start ();
// The engine starts to roar
EngineBoom ();
// Then start running. When a dog blocks the road, he honks the horn.
If (IsAlarm ())
Alarm ();

// Stop at the destination
Stop ();
}
Bool CHummerModel: IsAlarm ()
{
// Hook method. The default speaker will ring
Return true;
}

// HummerH1Model. h

# Pragma once
# Include "hummermodel. h"
Class CHummerH1Model:
Public CHummerModel
{
Public:
CHummerH1Model (void );
~ CHummerH1Model (void );
Void SetAlarm (bool tag );
Void Start ();
Void Stop ();
Void Alarm ();
Void EngineBoom ();
Bool IsAlarm ();
Private:
Bool m_isAlarm;
};
// HummerH1Model. cpp

# Include "StdAfx. h"
# Include "HummerH1Model. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CHummerH1Model: CHummerH1Model (void)
{
M_isAlarm = true;
}
CHummerH1Model ::~ CHummerH1Model (void)
{
}
Void CHummerH1Model: Start ()
{
Cout <"Hummer H1 launch..." <endl;
}
Void CHummerH1Model: Stop ()
{
Cout <"Hummer H1 parking..." <endl;
}
Void CHummerH1Model: Alarm ()
{
Cout <"Hummer H1 whistle" <endl;
}
Void CHummerH1Model: EngineBoom ()
{
Cout <"Hummer H1 engine sounds like this..." <endl;
}
Bool CHummerH1Model: IsAlarm ()
{
Return this-> m_isAlarm;
}
Void CHummerH1Model: SetAlarm (bool tag)
{
This-> m_isAlarm = tag;
}

// HummerH2Model. h

# Pragma once
# Include "hummermodel. h"
Class CHummerH2Model:
Public CHummerModel
{
Public:
CHummerH2Model (void );
~ CHummerH2Model (void );
Void Start ();
Void Stop ();
Void Alarm ();
Void EngineBoom ();
Bool IsAlarm ();
};
// HummerH2Model. cpp

# Include "StdAfx. h"
# Include "HummerH2Model. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CHummerH2Model: CHummerH2Model (void)
{
}
CHummerH2Model ::~ CHummerH2Model (void)
{
}
Void CHummerH2Model: Start ()
{
Cout <"Hummer H2 launch..." <endl;
}
Void CHummerH2Model: Stop ()
{
Cout <"Hummer H2 parking..." <endl;
}
Void CHummerH2Model: Alarm ()
{
Cout <"Hummer H2 whistle" <endl;
}
Void CHummerH2Model: EngineBoom ()
{
Cout <"Hummer H2 engine sounds like this..." <endl;
}
Bool CHummerH2Model: IsAlarm ()
{
Return false;
}

// TemplateMethod. cpp

# Include "stdafx. h"
# Include "HummerModel. h"
# Include "HummerH1Model. h"
# Include "HummerH2Model. h"
# Include <crtdbg. h>
Int _ tmain (int argc, _ TCHAR * argv [])
{
// The customer opened the H1 model and stepped out.
CHummerModel * ph1 = new CHummerH1Model ();
Ph1-> Run ();
Delete ph1;

// The customer opened the H2 model and went out to play.
CHummerModel * ph2 = new CHummerH2Model ();
Ph2-> Run ();
Delete ph2;

// When the customer opens the H1 model, the customer goes out for a bend and does not let the speaker Ring
CHummerH1Model * ph11 = new CHummerH1Model ();
Ph11-> SetAlarm (false );
Ph11-> Run ();
Delete ph11;

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();
Return 0;
}

The basic fixed logic is implemented by the Run of the base class, and different parts are encapsulated in the subclass. Run should not be overwritten by subclass. The template method belongs to the behavior mode. Relatively simple and common.

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.