未验证 提交 73849376 编写于 作者: L liu-jianhao 提交者: GitHub

template

上级 0616ff89
all:
g++ -g -Wall -o template1 template1_lib.cpp template1_app.cpp
g++ -g -Wall -o template2 template2_lib.cpp template2_app.cpp
clean:
rm -rf template1 template2
\ No newline at end of file
#include "template1_lib.cpp"
//Ӧó򿪷Ա
class Application
{
public:
bool Step2()
{
cout << "myStep2" << endl;
return true;
}
void Step4()
{
cout << "myStep4" << endl;
}
};
int main()
{
Library lib;
Application app;
lib.Step1();
if (app.Step2())
{
lib.Step3();
}
for (int i = 0; i < 4; i++)
{
app.Step4();
}
lib.Step5();
}
#include <iostream>
using namespace std;
//³ÌÐò¿â¿ª·¢ÈËÔ±
class Library
{
public:
void Step1()
{
cout << "Step1" << endl;
}
void Step3()
{
cout << "Step3" << endl;
}
void Step5()
{
cout << "Step5" << endl;
}
};
\ No newline at end of file
#include "template2_lib.cpp"
#include <iostream>
using namespace std;
//应用程序开发人员
class Application : public Library
{
protected:
virtual bool Step2()
{
//... 子类重写实现
cout << "override Step2" << endl;
return true;
}
virtual void Step4()
{
//... 子类重写实现
cout << "override Step4" << endl;
}
};
int main()
{
Library *pLib = new Application();
pLib->Run();
delete pLib;
}
#include <iostream>
using namespace std;
//程序库开发人员
class Library
{
public:
//稳定 template method
void Run()
{
Step1();
if (Step2())
{ //支持变化 ==> 虚函数的多态调用
Step3();
}
for (int i = 0; i < 4; i++)
{
Step4(); //支持变化 ==> 虚函数的多态调用
}
Step5();
}
virtual ~Library() {}
protected:
void Step1()
{ //稳定
cout << "Step1" << endl;
}
void Step3()
{ //稳定
cout << "Step3" << endl;
}
void Step5()
{ //稳定
cout << "Step5" << endl;
}
virtual bool Step2() = 0; //变化
virtual void Step4() = 0; //变化
};
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册