8.2 GOF设计模式一: 单实例模式 SingleTon
GOF设计模式一: 单实例模式 SingleTon
整个美国,只有一个“现任美国总统”
比如,在学校,“老师”,有数百个;“校长”,只有一个
系统运行时,如何保证某个类只允许实例化一个对象?
2.1 类的多重性
类的多重性 multiplicity
在对软件系统进行逻辑设计时,在某些情形之下,可能需要限制类的实例 在软件系统中存在的数目
多重性的图形表示
在类图上,类的多重性表达式被放置在类的图标的右上角
如果类的多重性表达式在类图上被省略,那么此类的多重性缺省为n,即 对此类的可同时存在的对象的数目没有限制
2.2 类的实例化
当类(class)不希望被实例化时
不提供构造函数
缺省的构造函数
定义成抽象类
要确保类不被实例化
类包含显式的构造函数
将构造函数声明为私有(private),在该类的外部就不可能访问,也就不能被实例化了
另外要注意的是,这种用法有点副作用,那就是它不能被子类化
因为 superclass 不可访问
2.3 单实例模式 The Singleton Pattern
Intent 目的
You want to have only one of an object, but there is no global object that controls the instantiation of this object.
You also want to ensure that all entities are using the same instance of this object,
without passing a reference to all of them
只希望有一个 对象,系统所有地方都可以用到这个对象,又不使用全局变量,也不需要传递对象的引用
Problem 问题
Several different client objects need to refer to the same thing, and you want to ensure that you do not have more than one of them
几个不同的客户对象都希望引用同一 个对象,如何保证?
Solution解决方案
Guarantees one instance 确保单实例
Participants and collaborators 参与方
Clients create an instance of the Singleton solely through the getInstance method
Consequences 结果
Clients need not concern themselves whether an instance of the Singleton exists. This can be controlled from within the Singleton
Implementation 实现
• Add a private static member of the class that refers to the desired object (Initially, it is null)
• Add a public static method that instantiates this class if this member is null (and sets this member's value) and then returns
the value of this member
• Set the constructor's status to protected or private so that no one can directly instantiate this class and bypass the static
constructor mechanism
2.3单实例模式 解决方法: Java为例
1、公有的成员函数,创 建并供客户获取该单实例
2、私有的构造函数
3、私有的静态成员变量
4、该单实例唯一的 创建之处
2.4 单实例模式 结构
单实例结构
锁住某类的实例化功能。客户类只能使用某类自身实例化的唯一实例
2.5 单实例模式 小结
单实例模式要点 There are two forces that affect the Singleton
只能有一个实例 There must be exactly one instance of the class
这个实例能够方便地被所有客户访问 The instance must be (easily) accessible to all potential clients
解决方法 Solution (Check list) 定义私有的静态成员变量,保存单实例的引用 Define a private static attribute in the "single instance" class
定义公有的Getter函数 Define a public static getter function in the class
该类自己负责“第一次使用时”实例化对象 Do "lazy initialization" (creation on first use) in the getter function
Class itself responsible for creating, maintaining, and providing global access to its own single instance
定义私有的构造函数 Define all constructors to be protected or private
客户对象只能通过 getter 函数获得该单实例
8.2 GOF设计模式一: 单实例模式 SingleTon的更多相关文章
- 设计模式之单实例模式(Singleton)
原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...
- xadmin系列之单实例模式
先看下单实例的定义 python的模块实现单例模式是python语言特有的,python的模块天然就是单例的,因为python有个pyc文件,导入一次后,第二次导入直接从pyc中取数据了 这里我们主要 ...
- Python静态方法实现单实例模式
单实例模式 当程序中需要同一个实例就可以解决问题的场景,可以使用单实例模式
- 23种设计模式之单例(Singleton Pattern)
单例 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例(eg:应对一些特殊情况,比如数据库连接池(内置了资源) 全局唯一号码生成器),才能确保它们的逻辑正确性.以及良好的效率 ...
- .Learning.Python.Design.Patterns.2nd.Edition之单实例模式
可以慢慢理解.. 对照JAVA class Singleton(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.inst ...
- 设计模式学习心得<单利模式 Singleton>
概述 意图 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 主要解决 一个全局使用的类频繁地创建与销毁. 何时使用 当您想控制实例数目,节省系统资源的时候. 如何解决 判断系统是否已经有这个单 ...
- 设计模式之单例(singleton)设计模式代码详解
单例有两种:懒汉式和饿汉式 /** * 懒汉式的单例模式 * 这种单例模式如果采用到多线程调用该方法,有可能会产生多个实例,原因是: * 当线程一进入了①处,此时轮到线程二的时间片,线程二也来到①处, ...
- [HBase Manual]CH5 HBase运行模式:单实例和分布式
HBase运行模式:单实例和分布式 HBase运行模式:单实例和分布式 1.单实例模式 1.1 单实例在HDFS下 2.分布式 2.1 伪分布式 3完全分布式 HBase有2种运行模式,单实例和分布式 ...
- C#设计模式-单实例
单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...
随机推荐
- 记录心得-FastJson分层解析demo示例
记录一下,平时用到,可速查!关键: // startArray(); 开始解析数组 // endArray(); 结束解析数组 // startObject(); 开始解析键值对 // endObje ...
- ASP.NET页面之间传值的方式之Cookie(个人整理)
Cookie Cookie 提供了一种在 Web 应用程序中存储用户特定信息的方法.例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息.当该用户再次访问您的网站时,应用程序 ...
- Lua 函数参数 & 默认实参
[1]Lua函数,默认实参 习惯了其他语言(如C++)的默认实参,利用Lua语言的过程中,发现没有默认实参这种机制. 所以,自己模拟了一个满足业务需求的带默认实参的函数. (1)示例如下: local ...
- flask 电子邮件Flask-Mail
电子邮件 在web程序中,经常会需要发送电子邮件.比如,在用户注册账户时发送确认邮件:定期向用户发送热门内容或是促销信息等等.在Web程序中发送电子邮件并不复杂,借助扩展Flask-Mail或是第三方 ...
- Easy methods to select MB Star, Extremely MB Star, MB SD C4, Mercedes BENZ C5 SD
MB Star, Extremely MB SD Connect C4, MB SD C4, Mercedes BENZ C5 SD are usually analysis tools to get ...
- IDEA Failed to load dx.jar
IDEA-177053 Android app crashes on build "Failed to load dx.jar" Error:Android Pre Dex: [c ...
- 用vue脚手架创建bootstrap-vue项目
用vue脚手架创建bootstrap-vue项目 框架的地址:https://bootstrap-vue.js.org/docs/ 第一步 vue init webpack demo第二步 cd de ...
- 要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10;
package text1; import java.util.ArrayList; import java.util.HashSet; /* * 要求产生10个随机的字符串, * 每一个字符串互相不 ...
- Python爬虫与一汽项目【三】爬取中国五矿集团采购平台
网站地址:http://ec.mcc.com.cn/b2b/web/two/indexinfoAction.do?actionType=showMoreCgxx&xxposition=cgxx ...
- JS对象格式化方法:pretty_format
/* * 格式化 * */ var pretty_format = function (obj, indent) { if (obj === null) return 'null'; if (obj ...