'디자인패턴'에 해당되는 글 1건

  1. 2014.01.08 [디자인패턴] cocos2d-x 싱글톤 싱글턴 패턴 구현

[디자인패턴] cocos2d-x 싱글톤 싱글턴 패턴 구현

|


#ifndef __SINGLETON_H__

#define __SINGLETON_H__

 

#include "cocos2d.h"

 

class SingletonClass{

private:

                  static SingletonClass * instance ;

                  ~SingletonClass(){/*empty*/};

                  SingletonClass(){

                                   value = 10;};

public:

                  static SingletonClass * getInstance();

                  static void releaseInstance();

 

                  int value;

                 

};

#endif

 

 

#include "Singleton.h"

 

SingletonClass * SingletonClass::instance = NULL ;

 

SingletonClass * SingletonClass::getInstance()

{

                  if (!instance)

                  {

                                   instance = new SingletonClass();

                  }

 

                 return instance;

}

 

void SingletonClass::releaseInstance()

{

                  if(instance) delete instance;

}

 



사용법

#include "Singleton.h"

CCLOG("VALUE %d", SingletonClass::getInstance()->value);

이런식으로 접근가능


Trackback 0 And Comment 0
prev | 1 | next