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;
    }


  • 相关阅读:
    1.4 build命令
    2.2-2 文章模块开发【添加文章页面脚本编写】
    2.2-1 文章模块开发 【入口脚本及模板的创建】
    2.1 开始一个项目 【功能梳理】
    [微信小程序]不在以下合法域名列表中
    [微信小程序]swiper保持宽高比
    爸爸一路走好
    LVM日记
    欲玩Discuz_X3.2,无奈不支持php7,再装个php5.3,编译到一半报错
    /sbin/ldconfig: /usr/local/lib64/libstdc++.so.6.0.22-gdb.py 不是 ELF 文件
  • 原文地址:https://www.cnblogs.com/wsh1230/p/8244251.html
Copyright © 2011-2022 走看看