Design Mode-singleton Mode

Source: Internet
Author: User

Category: Java

In some applications, one or more classes only need or can only have one instance. In this case, the Order Case mode is used. In windows, the spam recycle bin is a typical application, and there are many project configuration files. The system only needs one instance. If the system requires an instance of this type, the instance is directly returned if the instance of this type exists in the system. If the system does not have such an instance, create a new class instance to ensure that only one instance of this object exists in the system.

In the system, the class instance is created mainly by its constructor. Therefore, you need to control the class initialization process at this time. The following two examples generate a single instance of the required classes in different ways.

1:Hungry Chinese Style
 2:  public class EagerSingletonPattern {
 3:  
 4:  private static final EagerSingletonPattern ins = new EagerSingletonPattern();
 5:  
 6:  private EagerSingletonPattern(){
 7:      }
 8:  
 9:  public static EagerSingletonPattern getInstance(){
10:  return ins;
11:  }
12:  }

The following is the lazy style:

1:Lazy
 2:  public class LazySingletonPattern {
 3:  private static LazySingletonPattern ins = null;
 4:  
 5:  private LazySingletonPattern(){
 6:      }
 7:  
 8:  public synchronized static LazySingletonPattern getInstance(){
 9:  if( ins == null){
10:  ins = new LazySingletonPattern();
11:  }
12:  return ins;
13:  }
14:  }

When the class is loaded, an instance of the class is initialized. The system returns the instance directly when the class is required. The response speed is fast when the system is called, but the system resource utilization is low. When a class instance is needed, check whether the class instance already exists in the system. If yes, an instance is created. System resource utilization is high, but the response speed is slower when the system is called, and synchronization between threads is required in multithreading, it may take a long time to initialize the resource.

These two implementation methods can basically meet the required requirements. However, in the lazy implementation, subsequent threads need to wait for the execution method thread to complete before they can enter the execution, which undoubtedly prolongs the system response time. In the Head First design model, the author mentioned an improvement for lazy style. I personally think this method is correct, although Java and pattern mentioned in the book that dual checks into the correctness of examples. The following is an example of double check locking in Head First design patterns.

 1:  public class DCLSingletonPattern {
 2:  
 3:      private volatile static DCLSingletonPattern ins= null;
 4:  
 5:      private DCLSingletonPattern(){}
 6:  
 7:      public static DCLSingletonPattern getInstance(){
8: if (ins = null) {// The first check
9: synchronized (DCLSingletonPattern. class) {// Thread Synchronization
10: if (ins = null) {// second check
11:                      ins = new DCLSingletonPattern();
12:                  }
13:              }
14:          }
15:          return ins;
16:      }
17:  }

The volatile keyword ensures that after the variable ins is initialized to an instance, multiple threads can correctly process the ins variable.

The examples above all have more or less some defects. Their common problems are that they cannot be inherited by the quilt class. If the quilt class can be inherited, its construction method should at least be protect, rather than private. But if it is changed to protect, this class will lose the single-instance mode feature-other classes can directly call its constructor to generate a new instance. Therefore, the registration implementation method is provided in Java and mode. However, the subclass constructor cannot be private. Its constructor is open to the outside or partially open. It can only be instantiated according to the registration type to generate a single instance, and thus it is not perfect. Here, I will not give this example. In actual application, lazy and ELE. Me are basically enough.


This article is from the "on the road" blog, please be sure to keep this source http://dengxin919820.blog.51cto.com/4673187/1294664

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.