DesignPattern/23.TemplateMethodPattern/2.Code/Demo.h

42 lines
622 B
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#ifndef __DEMO_H__
#define __DEMO_H__
// 抽象类(基类)
class AbstractClass
{
public:
virtual ~AbstractClass(){}
// 模板方法,定义一个算法的框架流程
void templateMethod(){
// do something
method1();
method2();
method3();
}
// 基本方法——公共方法
void mehtod1(){
// do something
}
// 基本方法2
virtual void method2() = 0;
// 基本方法3——默认实现
void mehtod3(){
// do something
}
};
// 具体类(派生类)
class ConcreteClass :public AbstractClass
{
public:
// 实现基本方法2
void method2(){
// do something
}
// 重定义基本方法3覆盖基类的方法3
void method3(){
// do something
}
};
#endif