Simple Analysis of Singleton mode and simple analysis of Mode

Source: Internet
Author: User

Simple Analysis of Singleton mode and simple analysis of Mode

The first time I wrote something, I have a lot of styles and styles. please correct me.

This article aims to exchange and learn.

 

The Singleton mode is a simple one in the design mode and is easier to understand. Next we will talk about the singleton mode in various scenarios.

Definition of Singleton mode: A class has only one instance and is automatically instantiated and provided to the entire system.
Elements of Singleton mode: 1. private Static Instance Object 2. private Constructor (ensure that the object instance cannot be created outside the class through the new method) 3. public, static, and access to the Instance Object
The Singleton model is divided into lazy and ELE. me lazy

I. Lazy Singleton mode:

The advantage is that the Singleton class is not allocated space when loaded. The space is allocated only when the getInstance () method is called to save memory.
The disadvantage is that multiple Singleton instances may appear in the concurrent environment.

1/** 2 * lazy Mode 3 * The object 4 **/5 public class Singleton {6 7 private static Singleton instance = null is instantiated when the instance method is called for the first time; 8 private Singleton () {} 9 public static Singleton getInstance () {10 if (instance = null) {11 instance = new Singleton (); 12} 13 return instance; 14} 15 16 17}

Ii. Hunger Singleton mode:

Advantage: it is simple and there is no thread synchronization problem to avoid the resource waste caused by synchronized.

Disadvantage: the static class is loaded and allocated space, and the instance has been occupying resources since then.

1/** 2 * When a class is compiled and loaded, an object is instantiated and handed over to its own reference. 3 **/4 public class Singleton1 {
5 private Singleton1 () {6} 7 8 private static Singleton1 instance = new Singleton1 (); 9 10 public static Singleton1 getInstance () {11 return instance; 12} 13 14}

Iii. Lazy thread security Singleton mode:

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

 

Iii. multi-thread lazy mode:

Advantage: Multiple SingletonTest instances appear when the synchronized keyword is used to avoid multi-threaded access.
The disadvantage is that the efficiency is slightly lower when the synchronization method is frequently called.

1/** 2 * multi-thread lazy Mode 3 * locks the lazy Singleton mode to avoid errors caused by concurrency and ensure thread safety. 4 **/5 public class Singleton2 {6 private Singleton2 () {} 7 private static volatile Singleton2 instance = null; 8 public static synchronized Singleton2 getInstance () {9 if (instance = null) {10 synchronized (Singleton2.class) {11 if (instance = null) {12 instance = new Singleton2 (); 13} 14} 15} 16 return instance; 17} 18}

Use Cases:

  • Objects that need to be instantiated frequently and then destroyed
  • Objects that are time-consuming or resource-consuming but frequently used during object Creation
  • Objects that frequently access databases or files
  • Only one object is required

Advantages of 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.