Java Design Pattern modeling and implementation of cainiao series (6) Singleton pattern

Source: Internet
Author: User

Java Design Pattern modeling and implementation of cainiao series (6) Singleton pattern

Reprinted please indicate the source: http://blog.csdn.net/lhy_ycu/article/details/39784403


Singleton: a common design pattern. In Java applications, a singleton object can ensure that only one instance exists in one JVM. Benefits: 1. Some classes are frequently created. For some large objects, this is a huge system overhead. 2. Saves the new operator, reduces the usage frequency of the system memory, and reduces the GC pressure.

The Singleton mode can be implemented in two ways: 1. Lazy; 2. Hungry


I. uml modeling


Ii. Code implementation: 1. Lazy

/*** Example: a singleton-singleton object can ensure that only one instance exists in one JVM. ** Disadvantage: In a multi-threaded environment, this method is not safe ** lazy **/class Singleton {/*** to hold private static variables (also called class variables ), prevent being referenced ** here the value is null to achieve delayed loading (because some classes are large, so delayed loading helps improve performance) */private static Singleton instance = null; /** private constructor to prevent instantiation of */private Singleton () {}/** static factory method. Create an instance-only create yourself here, you can only create one */public static Singleton getInstance () {if (instance = null) {instance = new Singleton ();} return instance;} public void info () {System. out. println ("this is a test method... ") ;}}/*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {Singleton s1 = Singleton. getInstance ();/*** call common method */s1.info (); Singleton s2 = Singleton. getInstance ();/*** the running result is true, indicating that both class variables s1 and s2 point to the same object in memory */System. out. println (s1 = s2 );}}


2. Hungry Chinese
/*** Hunger style */class Singleton {private static Singleton instance = new Singleton (); private Singleton () {} public static Singleton getInstance () {return instance ;} public void info () {System. out. println ("this is a test method... ") ;}}/*** client Test class ** @ author Leo */public class Test {public static void main (String [] args) {Singleton s1 = Singleton. getInstance ();/*** call common method */s1.info (); Singleton s2 = Singleton. getInstance ();/*** the running result is true, indicating that both class variables s1 and s2 point to the same object in memory */System. out. println (s1 = s2 );}}

3. If multiple threads are considered, the getInstance () method must be synchronized with synchronized. In this case, the hunger style is better than the lazy one. Although the resource utilization is poor, synchronization is not required.

/***** When considering multithreading, the following practices can be referred to: -- lazy ** synchronization during class creation, so as long as the creation and getInstance () are separated, to create a synchronized keyword separately **, you only need to create an instance once for the entire program, so the performance will not be affected. ** @ Author Leo */public class SingletonTest {private static SingletonTest instance = null; private SingletonTest () {} private static synchronized void syncInit () {if (instance = null) {instance = new SingletonTest () ;}} public static SingletonTest getInstance () {if (instance = null) {syncInit () ;}return instance ;}}

Iii. Summary

The Singleton mode ensures that one class has only one instance, and provides a way to access global points. This ensures instance creation and access constraints more flexibly. There is only one instance in the system, so the constructor should be private: Create a static instance directly when the class is loaded; lazy: Create an instance when needed for the first time, the getInstance method is better than the lazy method to add synchronization hunger. Although the resource utilization is poor, the synchronization is not required.



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.