Dowemo

What's a singleton ( singleton ):

Some objects in the program I need only one, such as profile, tool class, log, cache, thread pool, etc.

When multiple programs are to call a configuration file, it's actually called a same profile; if multiple programs are called by multiple programs, there's a problem

For example, the cache log in a program, and so on, we just need one. We'll be using a singleton pattern if you want to ensure that one instance of the whole application is just one.


Apply scenario:

When some objects are only needed, use a singleton pattern


Action:

Make sure that one instance object in the entire program is exactly one


Type:


1. mode:


不管用户是否需要该对象,只要类加载了,就会去创建该单例模式的对象,即假设总是处于饥饿状态,所以就叫做饿汉模式






[java] view plain copy


  • public class Singleton {
  • //1. With co tructor, not allowing external objects to be created directly.
  • Create a co tructor and set the co tructor to private
  • This step modifies the default co tructor
  • private Singleton ( ) % 7b %
  • A//2. Creates a unique I & tance within the class, using the private static implementation ( because the co & tructor is kept in 1 ) with a private static implementation.
  • //static: turn it into a static member of a class to call outside
  • //private: to control access security, you need to turn it private and don't allow external access
  • When the class is loaded, it's initialized by static modifiers, which is reflected in mode.
  • private static Singleton singletonInstance = new A singleton ( );
  • A//3. Provides a method for obtaining an I & tance, using the public static implementation ( because I & ide the I & tances in 2 ).
  • You need to add static to a class method to be called by external. If you don't add static, it's an object, not a class method, so the external is accessed
  • Create a static public get function based on the encapsulation, so that you can get the instance directly by using the class name
  • public static Singleton getsingletoninstance ( ) % 7b
  • return A singletonInstance;
  • %.
  • %.
  • 
    

    2.懒汉模式:
    当用户第一次使用的时候才创建,后面再使用就不需要创建了,只实例化一次,即假设它比较懒,使用才创建,不用不创建,而且只创建一次,所以叫做懒汉模式
    [java] view plain copy
  • public class Singleton % 7b
  • //1. With co tructor, not allowing external objects to be created directly.
  • private Singleton ( ) % 7b %
  • //2. Declare only instances of the class, not I & tantiate.
  • No unique I & tance was created when class loads
  • private static A singleton singletonInstance;
  • //3. Provides a method for getting an I & tance.
  • The instance doesn't exist when the user gets the decision, the instance exists, the instance exists, and it can be returned directly, which also guarantees
  • The reflection of mode
  • public static Singleton getsingletoninstance ( ) % 7b
  • if( singletonInstance = = null) % 7b
  • SingletonInstance = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = _ new A singleton ( );
  • %.
  • return A singletonInstance;
  • %.
  • %.
  • 
    测试代码:

    [java] view plain copy
  • public class Test % 7b
  • The result of the console is: s1 = = s2
  • public static void Main ( string [ ] args ) % 7b
  • Singleton s1 = singleton. Getsingletoninstance ( ).
  • Singleton s2 = singleton. Getsingletoninstance ( ).
  • if( s1 = = s2 ) % 7b
  • System. Out. (. "s1 = = s2" );
  • %. else%.
  • System. Out. (. "s1! = s2" );
  • %.
  • %.
  • %.
  • 
    区别:

    饿汉模式:加载类使比较慢(每次都要创建实例),但是运行时获得对象的速度比较快(因为在类加载的时候就已经实例化对象),线程安全
    懒汉模式:加载类比较快(不需要创建实例),但是运行时获取对象的速度比较慢(因为每次都要去判断对象是否存在),线程不安全

    懒汉模式中的线程安全问题:
    从线程安全方面来考虑,不使用同步的懒汉模式是线程不安全的
    举例:假如从线程A开始进入,判断singletonInstance为空,此时就会创建一个实例对象,
    然后CPU进行切换,此时进来的是线程B,再对singletonInstance进行判断,依然为空,此时又会创建一个实例对象,
    然后CPU又切换到线程A继续创建实例,这时就会初相见两个实例
    所以为了安全的使用单例模式,要不选择使用饿汉模式在类加载时创建实例,要不就使用同步锁的懒汉模式

    懒汉模式线程安全的代码:
    使用的是双重检查锁定
    [java] view plain copy
  • public class Singleton % 7b
  • private Singleton ( ) % 7b %
  • private static A singleton singletonInstance;
  • public static Singleton getsingletoninstance ( ) % 7b
  • if ( singletonInstance = = null) % 7b
  • synchronized ( singleton. class) % 7b
  • if ( singletonInstance = = null) % 7b
  • SingletonInstance = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = _ new A singleton ( );
  • %.
  • %.
  • %.
  • return A singletonInstance;
  • %.
  • %.
  • 
    


    PS:
    仅是自己的见解,有不足的地方请见谅,欢迎大家留言讨论学习



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs