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

2024-05-08 09:18

本文主要是介绍设计模式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 模式的实例,因为系统我们就只要一个工厂来创建对象就可以了

这篇关于设计模式3--Singleton模式(单例模式)---创建型模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/969932

相关文章

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操