通过带C++语法高亮的editor软件阅读,将提高可读性和效率。这种editor软件有
(Win32)EditPlus2,UltraEdit32,Source Insigt;
(GNU/Linux)kedit, vim等。
类的关系:
Runnable <-- Application <-- SipToH323CallSigGW (箭头所指者为基类)
ApplicationCreator <-- SipToH323CallSigGWCreator
纯虚函数:
Runnable有run(),
ApplicationCreator有create()[@more@]//代码开始
ApplicationCreator* Application::myCreator = 0;
Application* Application::myApplication = 0;
int main() {
using namespace std;
cout << "Starting..." << endl;
SipToH323CallSigGWCreator creator;
Application::creator(&creator);
return ( Application::main() );
}
//代码结束
//代码开始
const int SUCCESS = 0;
typedef int ReturnCode;
//Application类
class Application : public Runnable {
private:
static Application* myApplication;
static ApplicationCreator* myCreator;
protected:
virtual ReturnCode init();
public:
static void creator(ApplicationCreator*);
static Application& instance();
static ReturnCode main();
};
//代码结束
//代码开始
void Application::creator(ApplicationCreator* creator) {
myCreator = creator;
}
Application& Application::instance() {
if ( !myApplication ) {
if ( myCreator != 0 ) {
myApplication = myCreator->create();
}
else {
throw "No ApplicationCreator";
}
}
return ( *myApplication );
}
ReturnCode Application::init() {
return SUCCESS;
}
ReturnCode Application::main() {
Application& app = Application::instance();
ReturnCode rc = 0;
try {
rc = app.init();
}
catch ( ... ) {
cerr << "Unhandled exception from Application::init()." << endl;
return 1;
}
if ( rc != 0 ) {
return rc;
}
try {
rc = app.run();
}
catch ( ... ) {
cerr << "Unhandled exception from Application::run()." << endl;
return 1;
}
return rc;
}
ReturnCode ServiceApplication::run() {
string fn ("ServiceApplication::Run");
cout << fn << endl;
ReturnCode rc = SUCCESS;
return rc;
}
ReturnCode ServiceApplication::init() {
cout << "ServiceApplication::init()" << endl;
ReturnCode rc = SUCCESS;
return rc;
}
Application* SipToH323CallSigGWCreator::create() {
return ( new SipToH323CallSigGW );
}
//代码结束
//代码重现开始
int main() {
using namespace std;
cout << "Starting..." << endl;
SipToH323CallSigGWCreator creator;
Application::creator(&creator);
return ( Application::main() );
}
//代码重现结束
通过Application::creator(),&creator传递给Applicaion::myCreator,myCreator指向creator(SipToH323CallSigGWCreator的实例)
在Appliation::main()中,先调用Application::instance(),
在Application::instance()中,调用myCreator->create()。由于myCreator指向creator,将调用creator的create()方法,该方法返回
一个SipToH323CallSigGW实例。而在Application::main()中,Application::instance()还将返回*myApplication,此时myApplication指
向生成的SipToH323CallSigGW实例,因而局部Application引用app也指向该实例。app.run()和app.init()调用的是ServiceApplication的
同名方法(多态^-^)。
欢迎通过illidan@mail.china.com交流、赐教。也可提供与本帖相关的源文件和GNU Makefile。
vovida的开源软件通过可了解更多。
--------------------next---------------------