惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
雷峰网
雷峰网
T
Tor Project blog
T
Tenable Blog
Spread Privacy
Spread Privacy
博客园 - 叶小钗
D
DataBreaches.Net
美团技术团队
A
Arctic Wolf
Project Zero
Project Zero
L
Lohrmann on Cybersecurity
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
Security Latest
Security Latest
D
Docker
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Secure Thoughts
T
Troy Hunt's Blog
U
Unit 42
WordPress大学
WordPress大学
I
Intezer
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
SecWiki News
SecWiki News
罗磊的独立博客
The Last Watchdog
The Last Watchdog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
B
Blog
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Microsoft Security Blog
Microsoft Security Blog
S
SegmentFault 最新的问题
The Cloudflare Blog
V
V2EX

博客园 - HenryRead

OpenGL 像素在内存中的排列方式 Unreal Engine 4 一些小技巧或提示 介绍Unreal Engine 4中的接口(Interface)使用C++和蓝图 Project Euler Problem 17 一种简单定义FourCC常量的方法 (C/C++) [翻译]进化游戏的层次结构 - 用组件来重构你的游戏实体 [转载]Netmsg 局域网聊天程序 [转载]使用 WSAAsyncSelect 的 Winsock 编程模型 [转载] VC6 STLport-5.1.4 /STLport-4.6.2 编译,安装 [转载] 跨平台C++程序开发系列文章 计算机科学数学理论浅谈 (转载) 致初学作曲的业余音乐爱好者 (转载) 搜集的优良OpenGL教程 (转载) OpenGL教程 "Top Ten" (转载) fltk2更新简介 FLTK简介 开发者:我们应该在哪个层次编写代码? [Linux 书籍合集] Linux eBooks collection CodeLite可以媲美Code::Blocks 不指定
[转载]Singleton的一个基类实现
HenryRead · 2010-07-14 · via 博客园 - HenryRead

今天去面试一家游戏公司,笔试题有道叫做 设计并实现一个Singleton基类。以前没有认真考虑过这个问题,转载了一篇。

原文地址:http://blog.csdn.net/Blue_Light/archive/2008/07/13/2646266.aspx

在创建型模式中,有一种设计模式“Singleton”。该模式的意图是,保证一个类仅有一个实例,并提供一个访问它的全局访问点。在GOF的指导下,我 们经常写一些Singleton类。每个类很类似。

   以下代码描述了一个Singleton的基类及使用方法:

template <class T>

class AllocUsingNew

{

public:

    static T* Create()

    {

       return new T;

    }

    static void Destroy(T *p)

    {

       delete p;

    }

};

template <class T>

class AllocUsingStatic

{

public:

    static T* Create()

    {

       static T t;

       return new (&t)T;

    }

    static void Destroy(T *p)

    {

       p->~T();

    }

};

template <class T,template <class> class AllocPolicy = AllocUsingStatic>

class Singleton

{

public:

    static T* Instance()

    {

       if (NULL == m_pThis)

       {

           m_pThis = AllocPolicy<T>::Create();

           assert(m_pThis);

           m_pThis->Initialize();

       }

       return m_pThis;

    }

    static void Destroy()

    {

       if (m_pThis)

       {

           m_pThis->Finalize();

           AllocPolicy<T>::Destroy(m_pThis);

           m_pThis = NULL;

       }

    }

    void Initialize()

    {

    }

    void Finalize()

    {

    }

protected:

    Singleton(){}

    ~Singleton(){}

private:

    Singleton(const Singleton&);

    Singleton& operator = (const Singleton&);

    friend class AllocPolicy<T>;

private:

    static T * m_pThis;

};

template <class T,template <class> class AllocPolicy>

T* Singleton<T,AllocPolicy>::m_pThis = NULL;

class MySingleton : public Singleton<MySingleton,AllocUsingNew>

{

public:

    void SetValue(int value)

    {

       m_value = value;

    }

    void print()

    {

       cout << m_value << endl;

    }

    void Initialize()

    {

       m_value = 1000;

    }

private:

    int m_value;

};

int _tmain(int argc, _TCHAR* argv[])

{

    MySingleton::Instance()->print();

    MySingleton::Destroy();

    system("pause");

    return 0;

}