1.设计模式:
2.设计模式分类:
3.单例模式:
4.代码:
饿汉模式
饿汉单例即最开始的时候, 静态对象就已经创建完成。
//
// main.cpp
// Singleton
//
// Created by 宋珂琦 on 2017/5/8.
// Copyright © 2017年 stand. All rights reserved.
//
#include <iostream>
using namespace std;
class Singleton {
//构造函数设为私有
Singleton() {
cout<<"singleton"<<endl;
}
//静态成员
static Singleton* instance;
public:
//提供静态共有方法
static Singleton* getSingleton() {
return instance;
}
};
Singleton* Singleton::instance = new Singleton();
int main() {
// insert code here...
cout<<"start main"<<endl;
//Singleton tmp;
//Singleton* ptr = new Singleton();
Singleton* pre1 = Singleton::getSingleton();
Singleton* pre2 = Singleton::getSingleton();
cout<<(pre1 == pre2)<<endl;//判断对象是否为同一个
return 0;
}
上面的main中, 我们主要对单例模式的特点进行了测试, 发现我们无法在其他地方再创建一个新的Singleton对象, 即下面两种方法都会失败, 这要归功于私有的构造函数。
//Singleton tmp;
//Singleton* ptr = new Singleton();
然后我们注意到刚进入main函数我们就打印了一句“start main", 因为我们此时的单例模式采用的是饿汉模式, 所以, 哪怕你还没用到这个对象, 也会先创建出来。 先占着, 其实就是全局变量的构造是进入main之前的原理。 还有最后的比较两个通过共有的静态成员方法返回的对象指针是否一致来验证我们单例模式返回的是同一个对象。
上面说的是饿汉模式, 这个模式有个不好的地方就是, 如果我一致不使用这个对象, 而这个对象是已经存在的, 它会一直占着内存, 所以比较懒的人就不愿意那么早准备好。
懒汉模式
所谓懒汉模式就是, 尽可能晚的创建这个对象的实例。即在单例类第一次被引用时将自己初始化。 其实c++里面很多地方都是类似这样的思想。 比如晚绑定, 写时拷贝技术等,就是尽量使资源的利用率最大化, 不要让空闲的人还占着资源。
//
// main.cpp
// Singleton
//
// Created by 宋珂琦 on 2017/5/8.
// Copyright © 2017年 stand. All rights reserved.
//
#include <iostream>
using namespace std;
class Singleton {
//构造函数设为私有
Singleton() {
cout<<"singleton()"<<endl;
}
//静态成员
static Singleton* instance;
public:
//提供静态共有方法
static Singleton* getSingleton() {
if(instance == NULL) {
instance = new Singleton();
cout<<"once"<<endl;
} else {
cout<<"it is not once"<<endl;
}
return instance;
}
};
Singleton* Singleton::instance = NULL;
int main() {
// insert code here...
cout<<"start main"<<endl;
//Singleton tmp;
//Singleton* ptr = new Singleton();
Singleton* pre1 = Singleton::getSingleton();
Singleton* pre2 = Singleton::getSingleton();
cout<<(pre1 == pre2)<<endl;//判断对象是否为同一个
return 0;
}
懒汉单例类和饿汉单例类的唯一不同之处就是,这个唯一的实例创建的时机不同,是一种按需分配的方式。
小结:
单例模式的实现过程中,需要注意以下三点:
1.单例类的构造函数为私有;
2.提供一个自身的静态私有成员变量;
3.提供一个共有的静态工厂方法。
----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------
5.单例模式的线程安全问题:
以懒汉单例为例,如果此时多线程进行操作,简单点以两个线程为例,假设pthread_1刚判断完instance为NULL为真,准备创建实例的时候,切换到了pthread_2,此时pthread_2也判断instance为NULL为真,创建了一个实例这是再切换回pthread_1的时候继续创建一个实例返回,那么此时就不会再满足单例模式的要求了,既然这样,是因为
多线程访问出现的问题,那么我们就来加把锁,使得线程同步;以伪代码的形式展现(省略pthread_mutex_lock/ulock):
static Singleton* getSingleton() {
lock(mutex) {
if(instance == NULL) {
instance = new Singleton();
cout<<"once"<<endl;
} else {
cout<<"it is not once"<<endl;
}
}
return instance;
}
那么我们写这样的代码是没有问题的,但是效率可能有点低,以为我们加锁的代价其实很大,那么有没有什么改进的地方可以尽量少的加锁呢?
这就是加锁的注意点,不同的加锁方式带来的效率可能完全不同,比如,此时我们如果再加锁前进行一个判断,if(instance ==NULL)的话,如果静态实例已经存在则不会进行加锁,同时也解决了上面所说的刚开始的时候多线程进行创建的同步问题;
伪代码如下:
static Singleton* getSingleton() {
if(instance == null) {
lock(mutex) {
if(instance == NULL) {
instance = new Singleton();
cout<<"once"<<endl;
} else {
cout<<"it is not once"<<endl;
}
}
}
return instance;
}
----------------------------------------------------------------------------------------------------------------
6.总结:
单例模式的优点:
提供了对唯一实例的受控访问。因为单例类封装了它的唯一实例。所以它可以严格的控制客户怎么样以及合适访问它;
由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁的创建和销毁的对象,单例模式无疑可以提高系统的性能。
允许可变数目的实例,基于单例模式我们可以扩展,使用与单例控制相似的方法来获得指定个数的对象实例。(即单例类内有多个静态对象指针成员,每次当单例类被引用时随机分配一个实例对象)。
单例模式的缺点:
因为单例模式没有抽象层,所以单例类的扩展有很大困难;
单例类的职责过重,既是工厂角色,提供了工厂的方法,同时又充当了产品的角色;
滥用单例类会带来一系列负面问题。
单例类的适用场景:
系统只需要一个实例对象,或者考虑到资源的消耗太大只允许创建一个对象。
客户调用类的单个实例只允许使用一个公共访问点,除了该访问点之外不允许通过其他方式访问该实例(就是共有的静态方法)。