`
yuanjinxiu
  • 浏览: 660141 次
文章分类
社区版块
存档分类
最新评论

第二人生的源码分析(7)应用程序实例类

 
阅读更多
在用C++设计一个应用程序时,总会使用应用程序实例类来管理所有事情,也就是说所有应用程序实例对象只有一个,并且保存这个应用程序所有状态,跟踪这个应用程序的变化。在第二人生里,也毫无例外地采用这种方案,现在就来分析一下应用程序实例类。它们的继承关系如下:
在文件llapp.h里定义类class LLApp
在文件llappviewer.h里定义类class LLAppViewer : public LLApp
在文件llappviewerwin32.h定义类class LLAppViewerWin32 : public LLAppViewer
通过浏览这三个头文件,就可以看到应用程序类的继承关系,这样设计可以灵活地适应不同的平台,又达到复用代码的目的。在Windows里是使用LLAppViewerWin32类,它是使用在Win32平台的封装。
如果是Linux平台,它就会使用到下面的类:
class LLAppViewerLinux : public LLAppViewer
这样就可适用于不同的平台运行和编译,又达到代码复用目的。
下面接着来分析类LLApp的几个重要函数:
#001//
#002// Main application logic
#003//
#004virtual bool init() = 0; // Override to do application initialization
#005
#006//
#007// cleanup()
#008//
#009// It's currently assumed that the cleanup() method will only get
#010// called from the main thread or the error handling thread, as it will
#011// likely do thread shutdown, among other things.
#012//
#013virtual bool cleanup() = 0; // Override to do application cleanup
#014
#015//
#016// mainLoop()
#017//
#018// Runs the application main loop.It's assumed that when you exit
#019// this method, the application is in one of the cleanup states, either QUITTING or ERROR
#020//
#021virtual bool mainLoop() = 0; // Override for the application main loop.Needs to at least gracefully notice the QUITTING state and exit.
#022
init()函数是用来进行应用程序初始化的功能。
cleanup()函数是清除创建的资源。
mainLoop()函数是主线程消息循环处理。
可以看到上面三个函数都是纯虚函数,因此全是接口的声明,所有的功能是在派生类里实现的。在使用这个类时,只需要调用相同的接口就可以适应不同的平台,不同的实现了,这样的过程就叫做面向对象的编程。在团队里不同开发人员之间,可以不用相互理解各个怎么样实现,只需要了解接口的功能,就可以了,这样也达到协同开发的目的。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics