본문 바로가기
C++/0x01-design pattern

design pattern ][ SingleTon

by SpeeDr00t 2016. 7. 14.
반응형

design pattern ][ SingleTon

1소스

#include <stdio.h>
#include <iostream>

using namespace std;

class SingleTon
{

public:
    static SingleTon* GetInstance()
    {
        if(NULL == pInstance)
        {
            pInstance = new SingleTon;
        }
        return pInstance;
    }

    static void ReleaseInstance()
    {
        delete pInstance;
        pInstance = NULL;
    }

    void TestPrint()
    {
        printf("Test SingleTon\n");
    }

private:
    SingleTon(){};
    ~SingleTon(){};
    static SingleTon* pInstance;
};

SingleTon* SingleTon::pInstance = NULL;

int main( int argc , char ** argv )
{
    SingleTon *s;
    SingleTon *st = s->GetInstance();
    st->TestPrint();
    st->ReleaseInstance();

return 0;
}

2.결과

hacker@HACKER:~/cpp$ g++ -o singleton singleton.cpp

hacker@HACKER:~/cpp$ ./singleton
Test SingleTon


반응형

'C++ > 0x01-design pattern' 카테고리의 다른 글

decorator pattern.  (0) 2016.07.14
composite pattern  (0) 2016.07.14
Observer pattern  (0) 2016.07.14
design pattern ][ Abstract Factory pattern  (0) 2016.07.14
state 패턴  (0) 2016.07.14