Java----Singleton mode

Source: Internet
Author: User

What is a singleton mode?

Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. The singleton schema definition in Java: "A class has and only one instance, and self-instantiation is provided to the system as a whole." ”

What is the meaning of the singleton pattern?

For some classes in the system, only one instance is important, for example, there can be multiple print tasks in a system, but only one task is working; a system can have only one window manager or file system; A system can have only one timing tool or ID (ordinal) generator. You can only open one task Manager in Windows. If you do not use the mechanism to unique window objects, will pop up more than one window, if these windows display exactly the same content, it is a duplicate object, wasting memory resources, if these windows display inconsistent content, it means that in a moment the system has multiple states, and the actual discrepancy, but also to the user misunderstanding, I don't know which one is the real state. Therefore, it is sometimes important to ensure that an object in the system is unique, that only one instance of a class is available.

Key points of the singleton model

  The main points of the singleton pattern are three; one is that a class can have only one instance, and the other is that it must create this instance on its own, and thirdly, it must provide this instance to the whole system on its own.

From the specific implementation point of view, is the following three points: first, the Singleton mode class only provides a private constructor, the second is that the class definition contains a static private object of the class, and third, the class provides a static public function to create or get its own static private objects.

The form of a singleton pattern (two common types are described here)

1. The first form: lazy (thread insecure).

  1. Public class Singleton {
  2. private static Singleton instance;
  3. Private Singleton () {}
  4. public static Singleton getinstance () {
  5. if (instance = = null) {
  6. Instance = new Singleton ();
  7. }
  8. return instance;
  9. }
  10. }

Lazy (thread-safe). It is inefficient and is used in very few cases.

  1. Public class Singleton {
  2. private static Singleton instance;
  3. Private Singleton () {}
  4. public static synchronized Singleton getinstance () {
  5. if (instance = = null) {
  6. Instance = new Singleton ();
  7. }
  8. return instance;
  9. }
  10. }

2. Second form: A Hungry man type
    1. Public class Singleton {
    2. private static Singleton instance = new Singleton ();
    3. Private Singleton () {}
    4. public static Singleton getinstance () {
    5. return instance;
    6. }
    7. }

A hungry man and lazy-type difference

From the name of the A hungry man and the lazy,

A hungry man is the class once loaded, the singleton initialization is completed, to ensure that the getinstance, the singleton is already exist,

And lazy idle, only when the call getinstance, only to go back to initialize the singleton.


Java----Singleton mode

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.