Foreword
For three factory model had Detailed understanding
now continue to learn other design mode
to create schema - singleton
table of Contents
- Singleton motivation
- Singleton defined
- Singleton Pattern structure
- Case realization
- Singleton disadvantages
- Mode for the environment
- Mode Application
- Extended mode
- to sum up
Singleton motivation
For some classes in the system, the guarantee only one instance is important
For example, in Windows Task Manager you can always open a
how to ensure that only one instance of a class, and this instance is easy to access it?
Define a global variable can be sure that the object can be accessed at any time, but can not prevent us instantiate multiple objects.
A better solution would be the only instance of the class to make itself responsible for saving it . This class can ensure that no other instance is created, and it can provide a way to access the instance
Singleton defined
- Singleton (Singleton Pattern): Singleton ensures that only one instance of a certain class, and to instantiate the instance of the entire system and to provide this class is called singleton class that provides global access method.
- Important single embodiment has three modes: First, only one instance of a class; second, it must create an instance of its own; it must provide their own three in this example the entire system. Singleton pattern is an object to create schema. Singleton and member list mode or single-mode state
Singleton Pattern structure
Singleton has only one role:
- Singleton class: instance comprising a self-propelled and can create an instance of the class
Singleton object is to ensure that only one instance of a class, and it provides access to a global access point . Role singleton pattern comprises only one, is the singleton class (Singleton).
单例类拥有一个私有构造函数,确保用户无法通过new关键字直接实例化它。除此之外,该模式中包含一个静态私有成员变量与静态公有的工厂方法,该工厂方法负责检验实例的存在性并实例化自己,然后存储在静态成员变量中,以确保只有一个实例被创建
案例实现
一个模拟的任务管理器
package com.company.DesignPatterns;
class TaskManager{
private static TaskManager taskManager=null;
private TaskManager() {
}
public static void getTaskManager(){
if (taskManager==null){
System.out.println("第一次打开任务管理器:允许打开");
taskManager=new TaskManager();
}
else {
System.out.println("已经打开了任务管理器:拒绝打开");
}
}
}
public class Singleton {
public static void main(String [] args){
System.out.println("第一次打开任务管理器");
TaskManager.getTaskManager();
System.out.println("第二次打开任务管理器");
TaskManager.getTaskManager();
}
}
单例模式优缺点
优点:
- 提供了对唯一实例的受控访问。因为单例类封装了它的唯一实例,所以它可以严格控制客户怎样以及何时访问它,并为设计及开发团队提供了共享的概念。
- 由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象,单例模式无疑可以提高系统的性能。
- 允许可变数目的实例。我们可以基于单例模式进行扩展,使用与单例控制相似的方法来获得指定个数的对象实例
缺点:
- 由于单例模式中没有抽象层,因此单例类的扩展有很大的困难。
- 单例类的职责过重,在一定程度上违背了“单一职责原则”。因为单例类既充当了工厂角色,提供了工厂方法,同时又充当了产品角色,包含一些业务方法,将产品的创建和产品的本身的功能融合到一起。
- 滥用单例将带来一些负面问题,如为了节省资源将数据库连接池对象设计为单例类,可能会导致共享连接池对象的程序过多而出现连接池溢出;现在很多面向对象语言(如Java、C#)的运行环境都提供了自动垃圾回收的技术,因此,如果实例化的对象长时间不被利用,系统会认为它是垃圾,会自动销毁并回收资源,下次利用时又将重新实例化,这将导致对象状态的丢失
模式适用环境
- 系统只需要一个实例对象,如系统要求提供一个唯一的序列号生成器,或者需要考虑资源消耗太大而只允许创建一个对象。
- 客户调用类的单个实例只允许使用一个公共访问点,除了该公共访问点,不能通过其他途径访问该实例。
- 在一个系统中要求一个类只有一个实例时才应当使用单例模式。反过来,如果一个类可以有几个实例共存,就需要对单例模式进行改进,使之成为多例模式
模式应用
- java.lang.Runtime类
Runtime类表示JVM运行时的一些运行环境
Runtime类定义的时候,它的构造方法已经被私有化了(单例设计模式的应用)
即必须保证运行只能有一个Runtime对象
私有构造方法和私有静态成员变量
-
数据库中表只能有一个主键
主键编号生成器必须具备唯一性,可以通过单例模式来实现 -
Spring中bean可以选择单例类型
<bean id="date" class="java.util.Date" scope="singleton"/>
模式扩展
Singleton 模式通常有两种实现形式:懒汉式单例、饿汉式单例
懒汉式单例
类加载时没有生成单例,只有当第一次调用 getlnstance 方法时才去创建这个单例
private static TaskManager taskManager=null;
意思是在成员变量这没有实例化,把实例化放在方法中
public static void getTaskManager(){
if (taskManager==null){
System.out.println("第一次打开任务管理器:允许打开");
taskManager=new TaskManager();
}
else {
System.out.println("已经打开了任务管理器:拒绝打开");
}
}
上面的案例就是懒汉式单例
饿汉式单例
Once loaded class creates a singleton to ensure that before calling the method getInstance Singleton has been in existence
private static TaskManager taskManager=new TaskManager();
That is, static member variables to instantiate an object
In learning to know JVM class loading, static member variables are created when the class is loaded
to sum up
- Singleton pattern is to ensure that only one instance of a class Object
- Singleton three points: only one instance of the class, the class instance to create their own, providing the Self class instance to the system
- Singleton private static member variable mode, a private constructor public static factory method implemented
- Singleton main advantage is to provide controlled access to the only examples and may save system resources; major drawback is that because of the lack of abstraction and difficult to extend, and heavy duty singleton class
- Example single-mode single mode is applied to the system embodiment requires only one object, only a single instance of a public access point, requires only one object class
- Singleton pattern implemented: starving single embodiment, lazy single embodiment