设计模式3--Singleton模式(单例模式)---创建型模式

        Singleton 模式解决问题十分常见,我们怎样去创建一个唯一的变量(对象)?在基于对象的设计中我们可以通过创建一个全局变量(对象)来实现,在面向对象和面向过程结合的设计范式(如 C++中)中,我们也还是可以通过一个全局变量实现这一点。但是当我们遇到了纯粹的面向对象范式中,这一点可能就只能是通过 Singleton 模式来实现了,可能这也正是很多公司在招聘 Java 开发人员时候经常考察 Singleton 模式的缘故吧。

//Singleton.h
#pragma once
#include <iostream>
using namespace std;
class Singleton
{
public:
	static Singleton* Instance();
protected:
	Singleton();
private:
	static Singleton* _instance;
};


//#include <boost/noncopyable.hpp>
//#include <enable_smart_ptr.h>
//
//#ifdef WIN32
//#include <boost/thread.hpp>
//#else
//#include <mutex>
//#include <thread>
//#endif
//
//
//template<class T>
//class enable_singleton : private boost::noncopyable
//{
//public:
//	static T& instance()
//	{
//#ifdef WIN32
//		boost::call_once(init, flag);
//#else
//		std::call_once(flag, init);
//#endif
//		return *t;
//	}
//
//	static void init()
//	{
//		t.reset(new T());
//	}
//
//	static void release()
//	{
//		t.reset();
//	}
//protected:
//	enable_singleton(){}
//	~enable_singleton(){}
//private:
//	enable_singleton(const enable_singleton& rhs) {}
//	enable_singleton& operator = (const enable_singleton& rhs) {}
//
//private:
//	static boost::scoped_ptr<T> t;
//#ifdef WIN32
//	static boost::once_flag flag;
//#else
//	static std::once_flag flag;
//#endif
//};
//
//template <class T> boost::scoped_ptr<T> enable_singleton<T>::t(nullptr);
//#ifdef WIN32
//template <class T> boost::once_flag enable_singleton<T>::flag = BOOST_ONCE_INIT;
//#else
//template <class T> std::once_flag enable_singleton<T>::flag;
//#endif


//#include <memory>
//#include <assert.h>
类模板
//template <class T>
//class Singleton
//{
//public:
//	Singleton()
//	{
//		if (msSingleton == nullptr)
//		{
//			msSingleton = static_cast<T*>(this);
//		}
//		else
//		{
//			assert(false);
//		}
//	}
//
//	virtual ~Singleton()
//	{
//		msSingleton = nullptr;
//	}
//
//	static T* getSingleton()
//	{
//		return msSingleton;
//	}
//private:
//	static T* msSingleton;
//};
//
//template <class T>
//T* Singleton<T>::msSingleton = nullptr;
//
//#define DeclareSingleton(classname)	\
//	public:	\
//	static classname*	getSingleton();	\
//	static void		release();	\
//	protected:	\
//	static std::auto_ptr<classname> msSingleton;	
//
//#define ImplementSingleton(classname)	\
//	std::auto_ptr<classname> classname::msSingleton(nullptr);	\
//	classname* classname::getSingleton()	\
//	{	\
//		if(msSingleton.get() == nullptr)	\
//		{	\
//			msSingleton.reset(new classname());	\
//		}	\
//		return msSingleton.get();	\
//	}	\
//	void classname::release()	\
//	{	\
//		if(msSingleton.get() != nullptr)	\
//		{	\
//			msSingleton.reset(nullptr);\
//		}	\
//	}	


//Singleton.cpp
#include "stdafx.h"
#include "Singleton.h"
#include <iostream>
using namespace std;
Singleton* Singleton::_instance = 0;
Singleton::Singleton()
{
cout<<"Singleton...."<<endl;
}
Singleton* Singleton::Instance()
{
if (_instance == 0)
{
_instance = new Singleton();
}
return _instance;
}

int main(int argc, _TCHAR* argv[])
{
	Singleton* sgn = Singleton::Instance();
	return 0;
}

       Singleton 模式的实现无须补充解释,需要说明的是,Singleton 不可以被实例化,因此我们将其构造函数声明为 protected 或者直接声明为 private。

     Singleton 模式在开发中经常用到,且不说我们开发过程中一些变量必须是唯一的,比如说打印机的实例等等。
    Singleton 模式经常和 Factory(AbstractFactory)模式在一起使用,因为系统中工厂对象一般来说只要一个,笔者在开发 Visual CMCS 的时候,语义分析过程(以及其他过程)中都用到工厂模式来创建对象(对象实在是太多了),这里的工厂对象实现就是同时是一个Singleton 模式的实例,因为系统我们就只要一个工厂来创建对象就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值