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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
罗磊的独立博客
人人都是产品经理
人人都是产品经理
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
L
Lohrmann on Cybersecurity
博客园 - 【当耐特】
量子位
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cyber Attacks, Cyber Crime and Cyber Security
腾讯CDC
有赞技术团队
有赞技术团队
Cyberwarzone
Cyberwarzone
T
Tor Project blog
V
V2EX
L
LINUX DO - 热门话题
Security Latest
Security Latest
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
NISL@THU
NISL@THU
C
Cisco Blogs
T
Tailwind CSS Blog
G
GRAHAM CLULEY
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
K
Kaspersky official blog
博客园 - 司徒正美
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
S
Schneier on Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
J
Java Code Geeks
博客园 - 聂微东
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
T
Threat Research - Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网

博客园 - 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;

}