Follow ZHONGHuan to learn the design mode-singleton Mode

Source: Internet
Author: User

Follow ZHONGHuan to learn the design mode-singleton Mode
Follow ZHONGHuan to learn the design mode.

As the object creation mode, the singleton mode ensures that a class has only one instance and is self-instantiated and provides this instance to the entire system. This class is called a singleton class.

Why is there only one instance:

For some classes in the system, it is very important to have only one instance. For example, a system may have multiple print tasks, but only one job is in progress; A system can have only one window manager or file system. A system can have only one timing tool or ID (serial number) generator. For example, only one task manager can be opened in Windows. If you do not use the window object uniqueness mechanism, multiple windows will pop up. If the content displayed in these windows is completely consistent, it is a duplicate object, wasting memory resources; if the content displayed in these windows is inconsistent, it means that the system has multiple States at a certain moment, which is inconsistent with the actual status. It may also cause misunderstandings to the user, and the user does not know which one is the real status. Therefore, it is important to ensure the uniqueness of an object in the system.

Class diagram:

In the class diagram, the getInstance () method returns the instance.

How to declare a singleton:

Hungry Chinese Style singleton:

Public class Singleton {privatestatic Singleton singleton = new Singleton (); // class is loaded to initialize the Singleton object. // This is called the hungry class because it is too anxious to privateSingleton () {} publicstatic Singleton getInstance () {returnsingleton ;}}


Lazy singleton:

Statement 1:

Public class Singleton {privatestatic Singleton singleton; privateSingleton () {}// note synchronized ensures thread security, meaning that only one thread can access this method at a time. publicstatic synchronized Singleton getInstance () {if (singleton = null) {// Initialization is performed only when singleton is used. The addition of judgment avoids repeated initialization and ensures that only singleton = new Singleton ();} returnsingleton ;}}


Writing Method 2: (comparison method 1, more efficient) is called double check.

Public class Singleton {privatestatic Singleton instance = null; privateSingleton () {// dosomething} publicstatic Singleton getInstance () {if (instance = null) {synchronized (Singleton. class) // you can think about why this improves the efficiency {if (null = instance) {instance = new Singleton () ;}} returninstance ;}}


Double check statement:
1. The first condition is that if an instance is created, it does not need to be synchronized. Simply return the result.

2 otherwise, we will start synchronizing the thread.

3. The second condition is that if an object is created in a synchronized thread, other threads do not need to be created.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.