上一节中,最后调用到了APPDelegate中的applicationDidFinishLaunching方法,
在applicationDidFinishLaunching方法中,获取到了Director的一个实例。
Director返回的是其子类DisplayLinkDirector的对象,其实现如下:
class Director { public: virtual ~Director() {} static Director* getInstance(); virtual bool init(); }; class DisplayLinkDirector : public Director { public: }; static DisplayLinkDirector *s_SharedDirector = NULL; Director* Director::getInstance() { if (!s_SharedDirector) { s_SharedDirector = new DisplayLinkDirector; s_SharedDirector->init(); } return s_SharedDirector; } bool Director::init() { return true; }
在Director的getInstance中,返回了一个静态的子类对象,并调用了其初始化方法。
在AppDelegate中调用:
#ifndef __APP_DELEGATE__ #define __APP_DELEGATE__ #include "Application.h" #include "Director.h" #include <iostream> class AppDelegate: private Application { public: virtual bool applicationDidFinishLaunching() { std::cout << "delegate applicationDidFinishLaunching" << std::endl; Director* director = Director::getInstance(); return true; } }; #endif
就添加了两个类Director和其子类DisplayLinkDirector。
附代码:demo3