Java Single example pattern learning example _java

Source: Internet
Author: User
Tags reflection

A single case pattern has some features:
1, Singleton class can only have one instance.
2. A singleton class must create its own unique instance itself.
3. A singleton class must provide this instance to all other objects.
The singleton pattern ensures that a class has only one instance and instantiates it itself and supplies the instance to the entire system. In computer systems, the driver objects of thread pools, caches, log objects, dialog boxes, printers, and graphics cards are often designed as single examples. These applications have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent two print jobs from being exported to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to avoid a single communication port being invoked simultaneously by two requests. In short, the choice of single case mode is to avoid inconsistent state, to avoid the administration of the Bulls.

First, look at a classic single example implementation.

Copy Code code as follows:

public class Singleton {

private static Singleton uniqueinstance = null;

Private Singleton () {

Exists only to defeat instantiation.

}

public static Singleton getinstance () {if (uniqueinstance = null) {

Uniqueinstance = new Singleton ();

}

return uniqueinstance;

}

Other methods ...
}

Singleton by restricting the construction method to private, the class is instantiated externally, and within the same virtual machine scope, the only instance of Singleton can be accessed through the getinstance () method. (In fact, the Java reflection mechanism is able to instantiate a class with a constructed method private, which essentially invalidates all Java Singleton implementations.) This question is not discussed here, but the reflection mechanism does not exist. )

However, the above implementation does not consider thread safety issues. Thread safety means that if you have more than one thread running in the process of your code, those threads may run the code at the same time. If the result of each run is the same as that of a single-threaded operation, and the value of other variables is the same as expected, it is thread-safe. Or, a class or program provides an interface that is atomic to a thread or a switch between multiple threads does not cause ambiguity in the execution of the interface, which means that we do not have to consider the problem of synchronization. Obviously the above implementation does not meet the requirements of thread safety, and multiple singleton instances are likely to occur in concurrent environments.

Copy Code code as follows:

A hungry man Singleton class. Is instantiated when class is initialized
public class Singleton1 {
Private default constructor
Private Singleton1 () {}
has been instantiated by itself
Private static final Singleton1 single = new Singleton1 ();
Static Factory method
public static Singleton1 getinstance () {
return single;
}
}

Lazy single case class. Instantiated at the first call
public class Singleton2 {
Private default constructor
Private Singleton2 () {}
Note that there is no final
private static Singleton2 Single=null;
Static Factory method
Public synchronized static Singleton2 getinstance () {
if (single = null) {
Single = new Singleton2 ();
}
return single;
}
}

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.