Creation design mode-singleton Mode

Source: Internet
Author: User

Creation design mode-singleton Mode

1: Singleton mode:

Ensure that there is only one instance in a class and provide a method to access its instance.

The most awesome Singleton mode is a dual test:
Class Singleton {private Singleton () {}; // private method private static Singleton instance = null; // defines the instance of the class as a static public Singleton getInstanSingleton () {if (instance = null) {// because synchronization is time-consuming, first determine whether it is null and then synchronize synchronized (Singleton. class) {// synchronize the class Object of Singleton; if (instance = null) {// because of the preceding synchronization, in multi-thread mode, other threads may have already created instances. Therefore, you must make another judgment at this time! Instance = new Singleton () ;}} return instance ;}}
Java source code example: Runtime. getRuntime (); returns the Runtime object related to the current Java application.
Calendar. getInstance (); obtain a Calendar in the current environment;
The application scenario is: resource management, such as windows recycle bin, only one.

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: 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.