zoukankan      html  css  js  c++  java
  • java设计模式-单例模式

    在软件工程中,单例模式是一种软件设计模式,它将一个类的实例化限制为一个对象。 当需要恰好一个对象来协调系统中的操作时,这非常有用。

    使用Singleton模式
       必须只有一个类的实例,并且必须可以从知名接入点访问客户端
      当唯一的实例应该通过子类来扩展时,客户端应该能够使用扩展实例而不修改它们的代码

    单例有好几种写法:

    1:

    /**
    * Private constructor so nobody can instantiate the class.
    */
    private IvoryTower() {}

    /**
    * Static to class instance of the class.
    */
    private static final IvoryTower INSTANCE = new IvoryTower();

    /**
    * To be called by user to obtain instance of the class.
    *
    * @return instance of the singleton.
    */
    public static IvoryTower getInstance() {
    return INSTANCE;
    }


    2:

    /**
    * Private constructor.
    */
    private InitializingOnDemandHolderIdiom() {}

    /**
    * @return Singleton instance
    */
    public static InitializingOnDemandHolderIdiom getInstance() {
    return HelperHolder.INSTANCE;
    }

    /**
    * Provides the lazy-loaded Singleton instance.
    */
    private static class HelperHolder {
    private static final InitializingOnDemandHolderIdiom INSTANCE =
    new InitializingOnDemandHolderIdiom();
    }


    3:

    private static ThreadSafeLazyLoadedIvoryTower instance;

    private ThreadSafeLazyLoadedIvoryTower() {
    // to prevent instantiating by Reflection call
    if (instance != null) {
    throw new IllegalStateException("Already initialized.");
    }
    }

    /**
    * The instance gets created only when it is called for first time. Lazy-loading
    */
    public static synchronized ThreadSafeLazyLoadedIvoryTower getInstance() {

    if (instance == null) {
    instance = new ThreadSafeLazyLoadedIvoryTower();
    }

    return instance;
    }


    4:

    private static volatile ThreadSafeDoubleCheckLocking instance;

    /**
    * private constructor to prevent client from instantiating.
    */
    private ThreadSafeDoubleCheckLocking() {
    // to prevent instantiating by Reflection call
    if (instance != null) {
    throw new IllegalStateException("Already initialized.");
    }
    }

    /**
    * Public accessor.
    *
    * @return an instance of the class.
    */
    public static ThreadSafeDoubleCheckLocking getInstance() {
    // local variable increases performance by 25 percent
    // Joshua Bloch "Effective Java, Second Edition", p. 283-284

    ThreadSafeDoubleCheckLocking result = instance;
    // Check if singleton instance is initialized. If it is initialized then we can return the instance.
    if (result == null) {
    // It is not initialized but we cannot be sure because some other thread might have initialized it
    // in the meanwhile. So to make sure we need to lock on an object to get mutual exclusion.
    synchronized (ThreadSafeDoubleCheckLocking.class) {
    // Again assign the instance to local variable to check if it was initialized by some other thread
    // while current thread was blocked to enter the locked zone. If it was initialized then we can
    // return the previously created instance just like the previous null check.
    result = instance;
    if (result == null) {
    // The instance is still not initialized so we can safely (no other thread can enter this zone)
    // create an instance and make it our singleton instance.
    instance = result = new ThreadSafeDoubleCheckLocking();
    }
    }
    }
    return result;
    }


  • 相关阅读:
    poj 2584 T-Shirt Gumbo (二分匹配)
    hdu 1757 A Simple Math Problem (乘法矩阵)
    矩阵之矩阵乘法(转载)
    poj 2239 Selecting Courses (二分匹配)
    hdu 3661 Assignments (贪心)
    hdu 1348 Wall (凸包)
    poj 2060 Taxi Cab Scheme (二分匹配)
    hdu 2202 最大三角形 (凸包)
    hdu 1577 WisKey的眼神 (数学几何)
    poj 1719 Shooting Contest (二分匹配)
  • 原文地址:https://www.cnblogs.com/wsh1230/p/8244251.html
Copyright © 2011-2022 走看看