头文件:
using namespace std;
//Model抽象基类
class CtemplatMode
{
public:
CtemplatMode();
};
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();
};
//Model衍生类1
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;
};
//Model衍生类2
class CHummerH2Model :
public CHummerModel
{
public:
CHummerH2Model(void);
~CHummerH2Model(void);
void Start();
void Stop();
void Alarm();
void EngineBoom();
bool IsAlarm();
};
// CTEMPLATMODE_H
源文件:
CtemplatMode::CtemplatMode()
{
}
CHummerModel::CHummerModel(void)
{
}
CHummerModel::~CHummerModel(void)
{
}
void CHummerModel::Run()
{
//先发动汽车
Start();
//引擎开始轰鸣
EngineBoom();
//然后就开始跑了,跑的过程中遇到一条狗挡路,就按喇叭
if (IsAlarm())
Alarm();
//到达目的地就停车
Stop();
}
bool CHummerModel::IsAlarm()
{
//钩子方法,默认喇叭是会响的
return true;
}
CHummerH1Model::CHummerH1Model(void)
{
m_isAlarm = true;
}
CHummerH1Model::~CHummerH1Model(void)
{
}
void CHummerH1Model::Start()
{
cout << "悍马H1发动..." << endl;
}
void CHummerH1Model::Stop()
{
cout << "悍马H1停车..." << endl;
}
void CHummerH1Model::Alarm()
{
cout << "悍马H1鸣笛" << endl;
}
void CHummerH1Model::EngineBoom()
{
cout << "悍马H1引擎声音是这样...." << endl;
}
bool CHummerH1Model::IsAlarm()
{
return this->m_isAlarm;
}
void CHummerH1Model::SetAlarm( bool tag )
{
this->m_isAlarm = tag;
}
CHummerH2Model::CHummerH2Model(void)
{
}
CHummerH2Model::~CHummerH2Model(void)
{
}
void CHummerH2Model::Start()
{
cout << "悍马H2发动..." << endl;
}
void CHummerH2Model::Stop()
{
cout << "悍马H2停车..." << endl;
}
void CHummerH2Model::Alarm()
{
cout << "悍马H2鸣笛" << endl;
}
void CHummerH2Model::EngineBoom()
{
cout << "悍马H2引擎声音是这样...." << endl;
}
bool CHummerH2Model::IsAlarm()
{
return false;
}
//使用测试
void templatTest()
{
//客户开着H1型号,出去遛弯了
CHummerModel *ph1 = new CHummerH1Model();
ph1->Run();
delete ph1;
//客户开H2型号,出去玩耍了
CHummerModel *ph2 = new CHummerH2Model();
ph2->Run();
delete ph2;
//客户开着H1型号,出去遛弯了,并且不让喇叭响
CHummerH1Model *ph11 = new CHummerH1Model();
ph11->SetAlarm(false);
ph11->Run();
delete ph11;
}