Mike's blog

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 23 Stories :: 83 Comments :: 0 Trackbacks

常用链接

留言簿(17)

我参与的团队

搜索

  •  

最新评论

所谓的单件类就是保证一个类仅有一个实例,并提供一个访问它的全局访问点。 

Singleton可以看作是一种经过改进的全局变量,既在一个进程中只能有唯一的实例,不允许产生第二个这样的对象。
虽然单件类是最简单的设计模式,但仍需小心使用,主要需注意:
1.构造函数
既然是只能有一个实例,那么构造函数自然不能被外部随意调用,所以需要将其声明为私有(private),包括默认构造、拷贝构造及赋值操作。至于是否需要实现要看具体应用。实例的产生需要一个辅助的成员函数(类似getInstance或creatInstance)。
2.析构函数
需要定义全局唯一的变量,我们首先会想到的就是静态(static),没错,单件类也是通过静态成员指针变量来实现单一。我们往往习惯于在析构函数中对成员指针进行内存释放,但在单件类中是不可以这样操作的,因为delete会调用类的析构,所以在自己的析构中delete自己的对象就会造成递归析构(无穷尽的析构现象)。
UML类图:

实现代码:
1)Singleton.hpp
 1/********************************************************************
 2* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
 3********************************************************************/

 4/*
 5 * @file Singleton.hpp
 6 * @brief  Declare the class of Singleton.
 7 * @version 0.1
 8 * @since 0.1
 9 * @author chenwei<76487974@qq.com> 
10 * @date 2010-7-19 Created it
11 */

12
13#ifndef _SINGLETON_HPP
14#define _SINGLETON_HPP
15
16#include <iostream>
17
18class Singleton
19{
20public:
21    ~Singleton() {
22        std::cout << "Singleton destructor." << std::endl;
23    }

24
25    static Singleton* creatInstance();
26    static void destroyInstance();
27    void test() {
28        std::cout << "Singleton test." << std::endl;
29    }

30
31private:
32    static Singleton* m_pInstance;
33
34    Singleton() {
35        std::cout << "Singleton constructor." << std::endl;
36    }

37
38    Singleton(Singleton&);
39    Singleton& operator=(Singleton&);
40}
;
41
42#endif
43
44

2)Singleton.cpp
 1/********************************************************************
 2* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
 3********************************************************************/

 4/*
 5 * @file Singleton.cpp
 6 * @brief  Implement the methods of the class Singleton.
 7 * @version 0.1
 8 * @since 0.1
 9 * @author chenwei<76487974@qq.com> 
10 * @date 2010-7-19    Created it
11 */

12
13#include "Singleton.hpp"
14#include <stdlib.h>
15
16Singleton* Singleton::m_pInstance = NULL;
17
18/**
19 * @fn creatInstance 
20 * @brief Create a Singleton instance.
21 * @return A pointer to Singleton Instance, or NULL if failed. 
22 * @author wei.chen (2010-7-19)
23 */

24Singleton* Singleton::creatInstance()
25{
26    std::cout << "Create the instance." << std::endl;
27    if (!m_pInstance) {
28        m_pInstance = new Singleton();
29        if (!m_pInstance) {
30            std::cout << "No memory to new for Singleton." << std::endl;
31            abort();
32        }

33    }

34
35    return m_pInstance;
36}

37
38/**
39 * @fn destroyInstance 
40 * @brief Release the memory for destroying the instance.
41 * @author wei.chen (2010-7-19)
42 */

43void Singleton::destroyInstance()
44{
45    std::cout << "Destroy the instance." << std::endl;
46    delete m_pInstance;
47    m_pInstance = NULL;
48}

49

3)Main.cpp
 1/********************************************************************
 2* Copyright (c) 2010~2010 All Rights Resverved by wei.chen.
 3********************************************************************/

 4/*
 5 * @file Main.cpp
 6 * @brief The entrance of the program.
 7 * @version 0.1
 8 * @since 0.1
 9 * @author chenwei<76487974@qq.com> 
10 * @date 2010-7-19    Created it
11 */

12
13#include "Singleton.hpp"
14
15/**
16 * @fn main 
17 * @brief The entrance of the program.
18 * @return int 
19 * @retval 0-normal 
20 * @author wei.chen (2010-7-19)
21 */

22int main()
23{
24    Singleton* singletonTest = Singleton::creatInstance();
25    if (!singletonTest) {
26        std::cout << "Create Instance failed." << std::endl;
27        return -1;
28    }

29
30    singletonTest->test();
31    Singleton::destroyInstance();
32
33    return 0;
34}

35

posted on 2010-07-19 23:39 老狼 阅读(1600) 评论(0)  编辑 收藏 引用 所属分类: C/C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理