Design Mode GOF23 Implementation of Singleton Mode

Source: Internet
Author: User

Design Mode GOF23 Implementation of Singleton Mode

Package com.bjsxt.cn. singleton;/*** features of the hungry Chinese-style singleton model *: * 1. Static attributes of private objects * 2. private constructor * 3, public static methods access static attributes (so the static environment is required) * @ author wanna **/public class SingletonDemo1 {// hungry, very hungry, when the class is initialized, the object is loaded immediately (without the capability of delayed loading ). If the loading class is very complicated, but it is not accessed later, causing the resource's wolf dung // because the loading class is naturally thread-safe, therefore, in the hungry Chinese Singleton mode, thread security is private static SingletonDemo1 instance = new SingletonDemo1 (); private SingletonDemo1 () {}// the access method is not synchronized with synchronized, therefore, the call efficiency is high. public static SingletonDemo1 getInstance () {return instance;

 }}



Package com.bjsxt.cn. singleton;/*** March 26, 2015 22:14:30 * lazy singleton mode * the lazy singleton Mode features: * 1. The constructor is private * 2, Private Static Property, and the type is the object of this class, but it is not initialized during the declaration. Because Lazy load. Because of laziness * 3, static methods for static access to the Private Static attribute. However, in this method, you must first check whether the static property is null. If it is null, new is returned. Otherwise, ** @ author wanna **/public class SingletonDemo2 {private SingletonDemo2 () is returned () {} private static SingletonDemo2 instance; // because the synchronous synchronized is added to the method, the efficiency is low, but the method has the ability to load at a time delay. Improved resource utilization. public static synchronized SingletonDemo2 getInstance () {if (instance = null) {instance = new SingletonDemo2 ();} return instance ;}}


Package com.bjsxt.cn. singleton;/*** March 26, 2015 22:14:38 ** test the dual detection locking singleton mode. * The features of the dual detection locking singleton mode are similar to those of the lazy mode, however, the most important difference is the process of accessing instances with static attributes **. However, due to underlying Compiler Optimization and JVM internal models, occasional problems may occur, it is not recommended to use * @ author wanna **/public class SingletonDemo3 {private static SingletonDemo3 instance = null; private SingletonDemo3 () {} // This method moves synchronized in the lazy Singleton mode down to if, which improves the execution efficiency and does not need to be synchronized every time an object is obtained. // Synchronization is not required after the first creation. Public static SingletonDemo3 getInstance () {if (instance = null) {SingletonDemo3 SC; synchronized (SingletonDemo3.class) {SC = instance; if (SC = null) {synchronized (SingletonDemo3.class) {if (SC = null) {SC = new SingletonDemo3 () ;}} instance = SC ;}} return instance ;}}



Package com.bjsxt.cn. singleton;/*** March 26, 2015 22:14:45 * single-instance mode for static internal Classes * This method is thread-safe, highly efficient, and can be loaded with latency * features: * 1, the constructor is private. The static attribute is changed to the static internal class, and the static final attribute of the external class is defined in the internal class (as if the deletion of final does not affect). * 3, the method for static access to static attributes returns the external encapsulated class object from the internal class in the method weight. The latency loading * @ author wanna **/public class SingletonDemo4 {

  private static class SingltonClassInstance {  private static SingletonDemo4 instance = new SingletonDemo4();  }  public static SingletonDemo4 getInstance() {  return SingltonClassInstance.instance;   } private SingletonDemo4() {   }}




package com.bjsxt.cn.singleton;

/*** Test enumeration to implement Singleton mode (without delay loading) * @ author wanna **/public enum SingletonDemo5 {Instance; // The enumeration element itself is a singleton object // Add the required public void operation (){}}

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.