特殊类设计

//只能在堆上创建对象的类
// 1. 构造函数私有
// 2. 提供一个静态的堆上创建对象的方法
// 3. 防拷贝(拷贝构造声明为私有,且不实现, 或者声明为delete函数)
class HeapOnly
{
public:
	static HeapOnly* getInstance()
	{
		return new HeapOnly;
	}
private:
	HeapOnly()
	{}

	HeapOnly(const HeapOnly& ho) = delete;
};

//只能在栈上创建对象的类
// a.
// 1. 构造函数私有
// 2. 提供一个公有的静态的栈上创建对象的方法
class StackOnly
{
public:
	static StackOnly getInstance()
	{
		return StackOnly();
	}
private:
	StackOnly()
	{}
};


//只能在栈上创建对象的类
// b.
// 1. 构造函数私有
// 2. 提供一个公有的静态的栈上创建对象的方法
// 3. 把operator new函数声明为delete函数
class StackOnly2
{
public:
	static StackOnly2 getInstance()
	{
		return StackOnly2();
	}

	void * operator new(size_t n) = delete;

private:
	StackOnly2()
	{}
};

//禁止拷贝的类
// 拷贝构造和赋值运算符声明为delete函数, 或者声明为私有的,且不实现
class NoCopy
{
public:
	NoCopy()
	{}

private:
	NoCopy(const NoCopy& nc) = delete;
	NoCopy& operator=(const NoCopy& nc) = delete;
};

//不能被继承的类
//1. 父类的构造声明为私有的
class NoH
{
private:
	NoH()
	{}
};

//或者父类定义为final类
class NoH2 final
{};

//饿汉模式
// 1. 构造函数私有
// 2. 提供一个静态的方法返回单例
// 3. 声明一个静态的单例成员
// 4. 拷贝构造和赋值声明为delete函数

// 特点: 实现简单, 多线程情景下效率高
// 缺点: 程序启动慢, 多个单例对象初始化的顺序无法控制
class singleton
{
public:
	static singleton* getInstance()
	{
		return &_single;
	}
private:
	singleton()
	{}

	singleton(const singleton& s) = delete;
	singleton& operator=(const singleton& s) = delete;
	static singleton _single;
};

//静态成员的初始化
singleton singleton::_single;



//懒汉模式
// 1. 构造函数私有
// 2. 提供一个静态的方法返回单例: 第一次调用, 创建对象, 后续调用直接返回
// 3. 声明一个静态的单例指针, 指针初始化为nullptr
// 4. 拷贝构造和赋值声明为delete函数
// 5. 保证线程安全(修改指针), 双检查提高效率

// 特点: 延迟加载,程序启动块, 可以指定多个单例对象的初始化顺序
// 实现复杂
class singleton2
{
public:
	static singleton2* getInstance()
	{
		if (_ptr == nullptr)
		{
			_mtx.lock();
				if (_ptr == nullptr)
				{
					_ptr = new singleton2;
				}
				_mtx.unlock();
		}
		return _ptr;
	}

	class GC 
	{
	public:
		~GC()
		{
			if (_ptr)
			{
				delete _ptr;
			}
		}
	};

private:
	singleton2()
	{}
	
	singleton2(const singleton2& s) = delete;
	singleton2& operator=(const singleton2& s) = delete;

	static singleton2* _ptr;
	static mutex _mtx;
	static GC _gc;
};

singleton2* singleton2::_ptr = nullptr;
mutex singleton2::_mtx;
singleton2::GC singleton2::_gc;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值