Java design pattern: Singleton mode

Source: Internet
Author: User

Concept:
  In Java, the singleton pattern is a common design pattern, there are several types of single-instance pattern, here are three kinds: lazy type single case, a hungry man type single case, registration type single case.
The singleton mode has the following characteristics:
  1, the Singleton class can have only one instance.
  2, the Singleton class must create its own unique instance.
  3. The Singleton class must provide this instance to all other objects.
Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system. In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.

One, the lazy one case

  1. Lazy type Singleton class. Instantiate yourself at the first call
  2. Public class Singleton {
  3. Private Singleton () {}
  4. private static Singleton single=null;
  5. //Static Factory method
  6. public static Singleton getinstance () {
  7. if (single = null) {
  8. Single = new Singleton ();
  9. }
  10. return single;
  11. }
  12. }

Second, a hungry man single case

  1. A hungry man Singleton class. When class is initialized, it is self-instantiated
  2. Public class Singleton1 {
  3. Private Singleton1 () {}
  4. private static final Singleton1 single = new Singleton1 ();
  5. //Static Factory method
  6. public static Singleton1 getinstance () {
  7. return single;
  8. }
  9. }

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.

1. Thread Safety:

A hungry man is inherently thread-safe and can be used directly for multithreading without problems.

The lazy type itself is non-thread-safe, in order to achieve thread safety there are several ways to write, respectively, the above 1, 2, 3, these three implementations in the resource load and performance of some differences.



2. Resource Loading and performance:

A hungry man a static object is instantiated at the same time as the class is created, and will occupy a certain amount of memory regardless of whether the singleton will be used later, but correspondingly, the speed will be faster on the first call because its resources have been initialized,

And the lazy type as the name implies, will delay loading, in the first use of the singleton when the object will be instantiated, the first call to do the initialization, if you want to do more work, performance will be somewhat delayed, and then the same as the A Hungry man type.

Java design mode: 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.