• 定义:确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例

    • image.png
  • 特点:只有一个实例;自我实例化;提供全局访问点

  • 优点:节约系统资源、提高系统效率,同时也能够严格控制客户对它的访问

  • 缺点:违背了“单一职责原则”,同时也没有抽象类,所以扩展起来有一定的困难;在并发测试中,不利于代码调试

  • 使用场合

    • 当在系统中某个特定的类对象实例只需要有一个的时候,可以使用单例设计模式。需要注意的是,只有真正有“单一实例”的需求时才可使用
    • Windows 的回收站、操作系统中的文件系统、多线程中的线程池、显卡的驱动程序对象、打印机的后台处理服务、应用程序的日志对象、数据库的连接池、网站的计数器、Web 应用的配置对象、应用程序中的对话框、系统中的缓存等常常被设计成单例
  • 多例模式的实现原理

    • 首先,在类中设置一个存储多个实例的容器,如ArrayList或HashMap;然后,静态初始化实例对象并添加到容器中;最后,根据条件返回容器中的实例对象

    • 用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象


1. 饿汉式—-转自阿翔,用于自学

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. using namespace std;
  5. // 单例模式:只允许实例化一个类
  6. // 饿汉式:main运行前就已经完成实例化
  7. class earth {
  8. int data;
  9. // 自己类型的一个静态成员
  10. static earth instance;
  11. // 构造函数为私有函数,外界不可调用
  12. earth(int val) : data(val) { printf("We create an earth\n"); }
  13. // 禁止拷贝构造
  14. earth(earth&) = delete;
  15. // 禁止拷贝
  16. earth& operator = (const earth&) = delete;
  17. void broadcast() {
  18. printf("We broadcast an message: Here is earth, my coordinates is solar system\n");
  19. }
  20. public:
  21. // 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)
  22. ~earth() { printf("We destroy an earth\n"); }
  23. // 获取实例,所有线程获取的都是同一个实例
  24. static earth& getInstance() {
  25. return instance;
  26. }
  27. void hello() {
  28. printf("This is earth say hello\n");
  29. }
  30. int read_data() {
  31. return data;
  32. }
  33. void reset(int val) {
  34. data = val;
  35. }
  36. void try_to_broadcast(int threashold) {
  37. if (data == threashold) {
  38. broadcast();
  39. } else {
  40. printf("You don't have any authority to activate broadcasting to universe\n");
  41. }
  42. }
  43. };
  44. // 静态变量的初始化,这个初始化早于main
  45. earth earth::instance(5);
  46. int main()
  47. {
  48. printf("We create an universe\n");
  49. earth &p = earth::getInstance();
  50. earth &q = earth::getInstance();
  51. p.hello();
  52. q.try_to_broadcast(7);
  53. q.reset(7);
  54. p.try_to_broadcast(7);
  55. printf("done\n");
  56. return 0;
  57. }

喊手撕的话可以写这个

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. using namespace std;
  5. class earth {
  6. private:
  7. // 自己类型的一个静态成员
  8. static earth instance;
  9. // 构造函数为私有函数,外界不可调用
  10. earth() { printf("We create an earth\n"); }
  11. // 禁止拷贝构造
  12. earth(earth&) = delete;
  13. // 禁止拷贝
  14. earth& operator = (const earth&) = delete;
  15. public:
  16. // 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)
  17. ~earth() {}
  18. // 获取实例,所有线程获取的都是同一个实例
  19. static earth& getInstance() {
  20. return instance;
  21. }
  22. };
  23. // 静态变量的初始化,这个初始化早于main
  24. earth earth::instance;
  25. /******************************************************************************/
  26. /******************************************************************************/
  27. # include<iostream>
  28. # include<cstdlib>
  29. using namespace std;
  30. class A {
  31. private:
  32. static A one;
  33. A() {}
  34. A(A&) = delete;
  35. A& operator = (const A&) = delete;
  36. public:
  37. ~A() {}
  38. static A& get() {
  39. return one;
  40. }
  41. };
  42. A A::one;

2. 懒汉式

版本1

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. using namespace std;
  5. // 单例模式:只允许实例化一个类
  6. // 懒汉式:只有在调用时刻才会实例化,没有调用就不会实例化
  7. // 解决了线程安全的问题,多线程同时进行首次调用时也保证只会实例化一次
  8. class earth {
  9. int data;
  10. // 指向自己实例的一个指针,初始化NULL
  11. static earth *instance;
  12. // 构造函数为私有函数,外界不可调用
  13. earth(int val) : data(val) { printf("We create an earth\n"); }
  14. // 禁止拷贝构造
  15. earth(earth&) = delete;
  16. // 禁止拷贝
  17. earth& operator = (const earth&) = delete;
  18. void broadcast() {
  19. printf("We broadcast an message: Here is earth, my coordinates is solar system\n");
  20. }
  21. public:
  22. // 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)
  23. // 此处不会掉起析构函数,原因未知。
  24. ~earth() { printf("We destroy an earth\n"); }
  25. // 获取实例,所有线程获取的都是同一个实例,并且只有在调用时才进行初始化
  26. // 多线程调用时,可能因为线程不同步的原因导致其被多次实例化的线程安全问题(这里不模拟这种情况
  27. static earth& getInstance() {
  28. if (instance == NULL) {
  29. instance = new earth(5);
  30. }
  31. return *instance;
  32. }
  33. void hello() {
  34. printf("This is earth say hello\n");
  35. }
  36. int read_data() {
  37. return data;
  38. }
  39. void reset(int val) {
  40. data = val;
  41. }
  42. void try_to_broadcast(int threashold) {
  43. if (data == threashold) {
  44. broadcast();
  45. } else {
  46. printf("You don't have any authority to activate broadcasting to universe\n");
  47. }
  48. }
  49. };
  50. // 将静态指针初始化为空
  51. earth* earth::instance = NULL;
  52. int main()
  53. {
  54. printf("We create an universe\n");
  55. earth& p = earth::getInstance();
  56. earth& q = earth::getInstance();
  57. p.hello();
  58. q.reset(7);
  59. p.try_to_broadcast(9);
  60. return 0;
  61. }

运行结果

  1. We create an universe
  2. We create an earth
  3. This is earth say hello
  4. You don't have any authority to activate broadcasting to universe

版本2

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. using namespace std;
  5. // 单例模式:只允许实例化一个类
  6. // 懒汉式:只有在调用时刻才会实例化,没有调用就不会实例化
  7. // 解决了线程安全的问题,多线程同时进行首次调用时也保证只会实例化一次
  8. class earth {
  9. int data;
  10. // 构造函数为私有函数,外界不可调用
  11. earth(int val) : data(val) { printf("We create an earth\n"); }
  12. // 禁止拷贝构造
  13. earth(earth&) = delete;
  14. // 禁止拷贝
  15. earth& operator = (const earth&) = delete;
  16. void broadcast() {
  17. printf("We broadcast an message: Here is earth, my coordinates is solar system\n");
  18. }
  19. public:
  20. // 析构函数不能定义为私有,外界可以调用析构函数,无法做到真析构(即内存不回收)
  21. ~earth() { printf("We destroy an earth\n"); }
  22. // 获取实例,所有线程获取的都是同一个实例,并且只有在调用时才进行初始化
  23. // 多线程调用时,编译器保证了函数进入后,初始化已经完成,不会进行多次实例化
  24. static earth& getInstance() {
  25. static earth instance(5);
  26. return instance;
  27. }
  28. void hello() {
  29. printf("This is earth say hello\n");
  30. }
  31. int read_data() {
  32. return data;
  33. }
  34. void reset(int val) {
  35. data = val;
  36. }
  37. void try_to_broadcast(int threashold) {
  38. if (data == threashold) {
  39. broadcast();
  40. } else {
  41. printf("You don't have any authority to activate broadcasting to universe\n");
  42. }
  43. }
  44. };
  45. int main()
  46. {
  47. printf("We create an universe\n");
  48. earth &p = earth::getInstance();
  49. earth &q = earth::getInstance();
  50. p.hello();
  51. q.reset(7);
  52. p.try_to_broadcast(9);
  53. return 0;
  54. }

运行结果

  1. We create an universe
  2. We create an earth
  3. This is earth say hello
  4. You don't have any authority to activate broadcasting to universe
  5. We destroy an earth