Singleton pattern)

Source: Internet
Author: User

Motivation:

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.

How can we ensure that a class has only one instance and this instance is easy to access? Defining a global variable ensures that objects can be accessed at any time, but it cannot prevent us from instantiating multiple objects.

A better solution is to let the class itself be responsible for saving its unique instance. This class can ensure that no other instance is created, and it can provide a method to access the instance. This is the model motivation of the singleton mode.

 

Intent:

Singletonpattern: singletonpattern ensures that a class has only one instance, and it is instantiated and provided to the entire system. This class is called singletonpattern, it provides global access methods.

The Singleton mode has three key points: one is that a class can only have one instance; the other is that it must create this instance; the third is that it must provide this instance to the entire system. The Singleton mode is an object creation mode. Singleton mode is also known as Singleton mode or Singleton mode.

 

UML diagram:

 

 

 

Role:

Singleton: defines a getinstance operation that allows customers to access its unique instance. getinstance is a class operation. You may be responsible for creating your own unique instance.

 

Mode Analysis:

The purpose of Singleton mode is to ensure that a class has only one instance and provide a global access point to it. The Singleton mode contains only one role, that is, Singleton. The Singleton class has a private constructor that prevents users from directly instantiating it using the new keyword. In addition, this mode contains a static private member variable and a static public factory method. The factory method is responsible for verifying the existence of the instance and instantiating itself, and then storing it in static member variables, to ensure that only one instance is created.

Public class Singleton

{


Privatestatic Singleton instance = NULL; // static private member variable


// Private constructor


Privatesingleton ()


{


}


// The static public factory method returns a unique instance


Publicstatic Singleton getinstance ()


{


If (instance = NULL)


Instance = new Singleton ();


Returninstance;


}

}

 

Note:Note the following three points when implementing the singleton mode:

1. the constructor of the singleton class is private;

2. Provide a static private member variable;

3. Provide a public static factory method.

 

Advantages and disadvantages:

Advantages:

1. controlled access to a unique instance is provided. Because the singleton class encapsulates its unique instance, it can strictly control how and when the customer accesses it and provide the shared concept for the design and development team.

2. Because only one object exists in the system memory, the system resources can be saved. For some objects that need to be created and destroyed frequently, the singleton mode can undoubtedly improve the system performance.

3. A variable target instance is allowed. We can expand based on the singleton mode and use methods similar to singleton control to obtain a specified number of object instances.

 

Disadvantages:

1. Because the singleton mode does not have an abstract layer, it is very difficult to expand the singleton class.

2. The responsibilities of the singleton class are too heavy, which violates the "single Responsibility Principle" to a certain extent ". Because the singleton class acts as a factory, provides factory methods, and acts as a product, including some business methods, integrate product creation with product functions.

3. Misuse of Singleton may cause some negative problems. For example, designing database connection pool objects as Singleton classes to save resources may lead to excessive programs sharing connection pool objects and connection pool overflow; currently, many runtime environments for object-oriented languages (such as Java and C #) provide the automatic garbage collection technology. Therefore, if the instantiated object is not used for a long time, the system will regard it as garbage and will automatically destroy and Recycle resources. The next exploitation will be re-instantiated, which will lead to the loss of object status.

 

Scope of use:

1. The system only needs one instance object. For example, the system requires a unique serial number generator, or you need to consider that the resource consumption is too large and only one object can be created.

2. A single instance of the customer call class can only use one public access point. Besides this public access point, the instance cannot be accessed through other channels.

3. In a system, only one instance of a class should be used. In turn, if a class can coexist with several instances, you need to improve the singleton mode to make it a multi-instance mode.

 

Mode extension:

1. Hunger examples

 

2. Lazy Singleton

 

 

Comparison between the hungry and lazy examples:

1. The hungry Chinese Singleton class instantiates itself when it is loaded. From the perspective of resource utilization efficiency, this is slightly worse than the lazy Singleton class. In terms of speed and response time, it is better than the lazy Singleton class.

2. when instantiating a lazy Singleton class, you must handle access restrictions when multiple threads reference this class for the first time. In particular, when a singleton class acts as a resource controller, resource Initialization must be involved in instantiation, and Resource Initialization may take a lot of time. This means that the probability of such a multi-thread being referenced for the first time is very high and requires control through the synchronization mechanism..

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.