Basic Knowledge (02)--Singleton mode

Source: Internet
Author: User

Single-Case mode

1. Basic Concepts

2. Definition of a singleton pattern

3. Roles in a singleton mode

4. Single-mode delay loading

5. Using static internal classes in Singleton mode to solve the problems of "lazy" and "a Hungry Man"

6. What is class-level inner class

----------------------------------------------------------------

1. Basic Concepts:

Singleton mode is the most common pattern used in design patterns, an object creation pattern that produces a concrete instance of an object that ensures that only one instance of a class is produced in the system.

There are two major benefits to using the Singleton pattern:

1. For frequently used objects, you can omit the time it takes to create an object

2. Due to the reduced number of new operations, the utilization of system memory is reduced, which reduces GC pressure and reduces GC downtime

2. Definition of the Singleton pattern:

Ensure that a class has only one instance and provides a global access point to access it

3. Roles in the singleton mode:

Singleton class: Providing a single factory, returning a single case

Use class: Get and use a singleton class

The following code is one of the simplest singleton modes:

1 /**2 * Demonstrates the simplest singleton mode (the following syntax will be problematic in multithreaded environments)3  */4  Public classSingleton {5     Private StaticSingleton instance =NewSingleton ();6     7     PrivateSingleton () {8System.out.println ("Singleton is create");9     }Ten      One      Public StaticSingleton getinstance () { A         returninstance; -     } -}

The core of the singleton pattern is the return of a unique object instance through an interface

Note The bold part of the above code:

1. The Singleton class must have a private access level constructor

The 2.instance member variable and the getinstance () method must be static

4. Lazy loading in a singleton mode

First look at the code:

/*** Demonstration of lazy loading in singleton mode *@authorAdministrator **/ Public classLazysingleton {Private StaticLazysingleton instance =NULL; PrivateLazysingleton () {System.out.println ("Lazysingleton is create!"); }        Private Static synchronizedLazysingleton getinstance () {if(Instance = =NULL) {instance=NewLazysingleton (); }        returninstance; }}

In the code implementation of the above singleton mode, although the function of deferred loading is implemented, the synchronization key synchronized is introduced, so it will be more time consuming in multi-threaded environment.

5. Using static internal classes in Singleton mode to solve the problems of "lazy" and "a Hungry Man"

Look at the code first:

1  Public classStaticsingleton {2     3     PrivateStaticsingleton () {4System.out.println ("Staticsingleton is create");5     }6Static Inner class7     Private Static classsingletonholer{8         Private StaticStaticsingleton instance =NewStaticsingleton ();9     }Ten      One      Public StaticStaticsingleton getinstance () { A         returnsingletonholer.instance; -     } -}

In the above implementation, Singleton mode uses an internal class to maintain a singleton instance, and when Staticsingleton is loaded, its inner class is not instantiated, so it is ensured that the Singleton class is not initialized when the Staticsingleton class is loaded into the JVM. When the getinstance () method is called, the Singletonholer is loaded and the instance is initialized, and because the instance is created at class load time, the JVM guarantees security under multithreading, so getinstance () Method also does not require the use of synchronization keywords.

6. What is class-level inner class

Simply put, the class-level inner class refers to a member-style inner class with static adornments. If there is no static adornment, the member inner class is called an object-level inner class.

The class-level inner class is equivalent to the static component of its outer class, which has no dependencies on the object and the outer class object, so it can be created directly. An instance of an object-level inner class is bound to an instance of an external object.

In a class-level inner class, you can define a static method. Only static member methods or member variables in an external class can be referenced in a static method.

Class-level internal classes are equivalent to members of their external classes and are loaded only when they are first used.

Basic Knowledge (02)--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.