应用程序结构框架允许从一个或一组类中继承以便创建一个新的应用程序
重用现存类中几乎所有的代码
并且覆盖其中一个或多个函数以便自定义所需要的应用程序
应用程序结构框架中的一个基本的概念是模板方法模式
//: C10:TemplateMethod.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Simple demonstration of Template Method.
#include <iostream>
using namespace std;
class ApplicationFramework {
protected:
virtual void customize1() = 0;
virtual void customize2() = 0;
public:
void templateMethod() {
for(int i = 0; i < 5; i++) {
customize1();
customize2();
}
}
};
// Create a new "application":
class MyApp : public ApplicationFramework {
protected:
void customize1() { cout << "Hello "; }
void customize2() { cout << "World!" << endl; }
};
int main() {
MyApp app;
app.templateMethod();
getchar();
} ///:~
输出
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
驱动应用程序进行的引擎 是模板方法模式
在GUI应用程序中
这个 引擎 是主要的事件环