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的更多相关文章

  1. 设计模式之单实例模式(Singleton)

    原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...

  2. xadmin系列之单实例模式

    先看下单实例的定义 python的模块实现单例模式是python语言特有的,python的模块天然就是单例的,因为python有个pyc文件,导入一次后,第二次导入直接从pyc中取数据了 这里我们主要 ...

  3. Python静态方法实现单实例模式

    单实例模式 当程序中需要同一个实例就可以解决问题的场景,可以使用单实例模式

  4. 23种设计模式之单例(Singleton Pattern)

    单例 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例(eg:应对一些特殊情况,比如数据库连接池(内置了资源)  全局唯一号码生成器),才能确保它们的逻辑正确性.以及良好的效率 ...

  5. .Learning.Python.Design.Patterns.2nd.Edition之单实例模式

    可以慢慢理解.. 对照JAVA class Singleton(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.inst ...

  6. 设计模式学习心得<单利模式 Singleton>

    概述 意图 保证一个类仅有一个实例,并提供一个访问它的全局访问点. 主要解决 一个全局使用的类频繁地创建与销毁. 何时使用 当您想控制实例数目,节省系统资源的时候. 如何解决 判断系统是否已经有这个单 ...

  7. 设计模式之单例(singleton)设计模式代码详解

    单例有两种:懒汉式和饿汉式 /** * 懒汉式的单例模式 * 这种单例模式如果采用到多线程调用该方法,有可能会产生多个实例,原因是: * 当线程一进入了①处,此时轮到线程二的时间片,线程二也来到①处, ...

  8. [HBase Manual]CH5 HBase运行模式:单实例和分布式

    HBase运行模式:单实例和分布式 HBase运行模式:单实例和分布式 1.单实例模式 1.1 单实例在HDFS下 2.分布式 2.1 伪分布式 3完全分布式 HBase有2种运行模式,单实例和分布式 ...

  9. C#设计模式-单实例

    单例模式就是保证在整个应用程序的生命周期中,在任何时刻,被指定的类只有一个实例,并为客户程序提供一个获取该实例的全局访问点. 1.经典的模式 namespace singleClass { class ...

随机推荐

  1. PHP菜鸟如何开始学习PHP语言

    把我自己学习PHP的经验分享出来,既给想学习PHP的朋友提供一个思路,也算是整理一下自己的思路,好给后续的教程开个头吧~ 学习其实也是有方法的,举个例子:在您上学期间,班里一定有学霸,也有学渣,也有普 ...

  2. redis知识点汇总

    1. redis是什么 2. 为什么用redis 3. redis 数据结构 4. redis中的对象类型 5. redis都能做什么?怎么实现的的? 6. redis使用过程中需要注意什么 7. 数 ...

  3. Java里面使用Date.compareTo比较时间

    DateFormat dateFormat = DateFormat.getDateInstance(); Date oldTime = dateFormat.parse("2019-04- ...

  4. fastjson的简单使用

    实习初期,开始接触到系统的后台日志查询展示到前台,后台将查询到的数据转化成json格式返回前台,发现系统使用的是阿里的fastjson,于是想来了解一下fastjson的使用. 以下使用都是基于这个版 ...

  5. php 把数组保存为标准的数组格式,存储到文件中

    <?php $file='./test.php'; $array=array('color'=> array('blue','red','green'),'size'=> array ...

  6. [Python]基础教程(4)、Python 变量类型

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  7. 如何使用postman传数组数据

    如何使用postman传数组数据 在我们做api接口数据调试的时候,大部分是会用到postman的,一般请求数据的参数都是字符串,但是特殊情况下我们是需要传一个数组数据的,那么为了实现这种需求,究竟该 ...

  8. freeswitch 事件命令

    1.uuid_bridge 桥接两条呼叫的腿. Usage: uuid_bridge <uuid> <other_uuid> uuid_bridge至少需要有一条腿是被呼通的. ...

  9. Java基础学习-关键字的概述和特点以及常量的概述和分类

    1.关键字概述     -被Java语言赋予特定含义的单词 2.关键字的特点     -组成关键字的字母全部小写     -常用的代码编辑器,针对关键字有特殊的颜色标记,非常直观,所以我们不需要死记硬 ...

  10. linux基础之CentOS7新特性

    CentOS7开机启动顺序: POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> ...