Singleton pattern)

Source: Internet
Author: User

Java mode-singleton mode: Singleton mode ensures that a class has only one instance, and provides the instance to the entire system. Features: 1. A class can only have one instance. 2. Create this instance by yourself. 3. Use this instance for the entire system: In the following object diagram, there is a "singleton object", and "Customer A", "Customer B", and "customer C" are the three customer objects of the singleton object. As you can see, all customer objects share one singleton object. In addition, we can see from the singleton object to its own connection line that the singleton object holds a reference to itself. The Singleton mode ensures that only one instance of a class exists in a Java application. In many operations, such as the creation of Object-database connections, such single-threaded operations are required. Some resource managers are often designed to work in singleton mode. External resources: for example, each computer may have several printers, but only one printer spooler can be used to prevent two print jobs from being output to the printer at the same time. Each computer can have several communication ports. The system should centrally manage these communication ports to prevent a communication port from being called by both requests at the same time. For example, most software has one or more attribute files to store system configurations. Such a system should be managed by an object. Example: Windows recycle bin. In the entire Windows system, the recycle bin can only have one instance, and the entire system uses this unique instance, and the recycle bin provides its own instance. Therefore, the recycle bin is a singleton application. Two forms: 1. the hungry Chinese Singleton class public class Singleton {
 
Private Singleton (){}
 
// Define your own instance internally. Isn't it strange?
// Note that this is private for internal calls only
 
Private Static Singleton instance = new Singleton ();
 
// Here is a static method for external access to this class, which can be accessed directly.
Public static Singleton getinstance (){
Return instance;
}
} 2. Lazy Singleton type

Public class Singleton {

Private Static Singleton instance = NULL;

  

  
If (instance = NULL)
Instance = new Singleton ();
Return instance ;}

}

// This method is better than above. You don't need to generate objects every time. It's just the first time.
// Generate instances during use, improving efficiency! Public static synchronized Singleton getinstance () {The second form is lazy initialization. That is to say, the initial Singleton for the first call will not be regenerated in the future. Note that synchronized in the form of lazy Initialization is important. If synchronized is not available, you may obtain multiple Singleton instances by using getinstance. Generally, the first type is safer.

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.