Love life, enjoy entertainment, focus on technology, welcome to the public Qger, we witness growth together!
What is a singleton mode?
- A class has and has only one instance, which is instantiated by the system itself and provided to the entire system through a global access point.
Why use singleton mode?
- Saves memory and does not need to instantiate an object every time it is used
- One instance provides global reuse
- In some environments, it is important to guarantee that a class has and only one instance, such as under Windows Task Manager.
How do I use Singleton mode?
A single example of the pattern of strange, all kinds of writing have, finally escaped the following three points:
- Have and have only one instance
- No manual creation, self-instantiation of the system
Provides access to the entire system through a single global access point
In addition, there may be different requirements in different environments: ensure thread safety, ensure serialization, do not produce multiple instances when deserializing, multiple instances of the ClassLoader when loaded, and so on.
The first implementation of:
publicclass Singleton { privatestaticnull; privateSingleton(){ } publicstaticgetInstance(){ ifnull){ new Singleton(); } return singleton ; }}
这一种单例的实例化是在需要用到时才进行实例化,但有明显的缺点:1、线程不够安全 2、每次使用都需要判断是否已经实例化,第一个缺点可以使用 synchronized 来声明 getInstance方法。第二个缺点可以使用第二种实现来避免。
The second implementation:
publicclass Singleton { privatestaticnew Singleton(); privateSingleton(){ } publicstaticgetInstance(){ return singleton ; }}
这一种单例的实例化基于classloader机制,避免了多线程安全的方式,同时也不需要在实例每一次使用时判断是否已经实例化,属于最常用的方式。类似实现还可以使用静态块来进行单例的实例化,不要累述。
The third implementation:
publicclass Singleton { privatestaticclass SingletonHolder { privatestaticnew Singleton(); } privateSingleton (){} publicstaticgetInstance() { return SingletonHolder. INSTANCE; } }
这一种实现Singleton类被装载了,instance不一定被初始化。因为SingletonHolder类没有被主动使用,只有显示通过调用getInstance方法时,才会显示装载SingletonHolder类,从而实例化instance。
The fourth implementation:
publicenum Singleton{ SINGLETON; publicvoiddoWhatever(){};}
这一种实现使用枚举来实现,能避免多线程同步问题,还能防止反序列化重复创建对象的问题,但由于枚举是在jdk1.5才加入的新特性,因此目前还很少看到有人在实际项目中使用。
The fifth implementation:
public Class Singleton {private volatile static Singleton Singleton; private singleton () {} public static Singleton getsingleton () {if (singleton = = Null ) {synchronized (Singleton. Class) {if (Singleton = = null ) {singleton = new Singleton (); }}} return Singleton; } }
has been abandoned the use of a writing, specific can be stamped ....
http://blog.csdn.net/kufeiyun/article/details/6166673
In addition, in reading Uncle Bob's article about Singleton and "create only one instance", he offers another way to do this:
publicclass Singleton { privateSingleton(){} publicstaticnew Singleton());}publicclass NonSingleton { publicNonSingleton(Singleton singleton) { null); } publicvoiddoWhatever(){ }}
这一种实现主要是因为uncle bob 认为单例模式自身违背了SRP(单一职责原则)即一个类只做一件事的原则,而他的这种实现即是将单例实例化、以及单例做的事情分割开来。
About design mode: Singleton mode (Singleton)