听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • Python

  • Go

  • 数据结构与算法

  • 设计模式

    • 【设计模式】1 引言
    • 创建型

      • 【设计模式】创建型模式-Abstract Factory模式
      • 【设计模式】创建型模式-Builder模式
      • 【设计模式】创建型模式-Factory模式
      • 【设计模式】创建型模式-Prototype模式
      • 【设计模式】创建型模式-Sington模式
    • 结构型

    • 行为型

  • 程序设计层
  • 设计模式
  • 创建型
zfprotectors
2022-05-18

【设计模式】创建型模式-Sington模式

这个模式设计到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建,提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。 注意:

  • 单例类只能有一个实例
  • 单例类必须自己创建自己的唯一实例
  • 单例类必须给所有其他对象提供这一实例

# 功能

保证一个类仅有一个实例,并提供一个它的全局访问点

# 解决

  • 主要解决:一个全局使用的类频繁地创建与销毁
  • 何时使用:当你想要控制实例数目,节省系统资源的时候
  • 如何解决:判断系统是否已经有这个单例,如果有则返回,如果没有则创建
  • 关键代码:构造函数是私有的

# 优缺点

  • 优点:
    • 在内存中只有一个实例,减少内存的开销(例如首页页面缓存)
    • 避免对资源的多重占用(写文件操作)
  • 缺点:
    • 没有接口,不能继承
    • 与单一职责原则冲突

# 应用场景

  • 应用实例:
    • 一个班级只有一个班主任
    • 所有文件的处理必须通过唯一的实例来进行
  • 使用场景:
    • 要求生产唯一序列号
    • WEB中的计数器,不用每次刷新都在数据库里加一次,用单例先缓存起来
    • 创建的一个对象需要消耗的资源过多,比如I/O与数据库的连接等
  • 注意事项:复杂对象适合使用工厂模式,而简单对象使用工厂模式反而会增加复杂度

# 简单示例代码部分

Sington.hpp

#ifndef _SINGLETON_H_
#define _SINGLETON_H_
#include <iostream>
using namespace std;
class Singleton{
private:
    static Singleton* _instance;
    Singleton(){
        cout<<"Singleton..."<<endl;
    }
    Singleton(const Singleton& other);
public:
    static Singleton* Instance();
};
Singleton* Singleton::_instance=0;
//线程非安全版本
Singleton* Singleton::Instance(){
    if(_instance==0){
        _instance=new Singleton();
    }
    return _instance;
}
/*线程安全版本,但锁的代价过高
Singleton* Singleton::Instance(){
    Lock lock;
    if(_instance==0){
        _instance=new Singleton();
    }
    return _instance;
}
*/
/*双检查锁,但由于内存读写reorder不安全,造成双检查锁失效
锁前不检查:代价过高
锁后不检查:锁不住
Singleton* Singleton::Instance(){
    if(_instance==0){
        Lock lock;
        if(_instance==0){
            _instance=new Singleton();
        }
    }
    return _instance;
}
*/
/*C++ 11 版本之后的跨平台实现(volatile)
std::atomic<Singleton*> Singleton::_instance;
std::mutex Singleton::_mutex;
Singleton* Singleton::Instance(){
    Singleton* tmp = _instance.load(std::memory_order_relaxed);
    std::atomic_thread_fence(std::memory_order_acquire);//获取内存fence
    if(tmp==nullptr){
        std::lock_guard<std::mutex> lock(_mutex);
        tmp=_instance.load(std::memory_order_relaxed);
        if(tmp==nullptr){
            tmp = new Singleton;
            std::atomic_thread_fence(std::memory_order_release);//释放内存fence
            _instance.store(tmp, std::memory_order_relaxed);
        }
    }
    return tmp;
}
*/
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

main.cpp

#include "Singleton.hpp"
#include <iostream>
using namespace std;
int main(){
    Singleton* sgn=Singleton::Instance();
    Singleton* sgn2=Singleton::Instance();
    return 0;
}
1
2
3
4
5
6
7
8
编辑 (opens new window)
#设计模式
上次更新: 2022/05/18, 15:47:37
【设计模式】创建型模式-Prototype模式
【设计模式】结构型模式-Adapter模式

← 【设计模式】创建型模式-Prototype模式 【设计模式】结构型模式-Adapter模式→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式