java 单例模式构造函数需要私有吗?

本文讨论了单例模式中构造函数是否应该私有化的问题,并通过分析Glide、EventBus及Runtime等实例,揭示了单例模式的应用场景及其背后的考虑。

先看下标准的单例模式之一的代码写法,单例模式有几种写法,如下:

public class SocketUtil {
    
    private static volatile SocketUtil socketUtil;

    /**
     * socketUtil 单例模式 - 双重校验锁
     */
    public static SocketUtil getInstance() {
        if (socketUtil == null) {
            synchronized (SocketUtil.class) {
                if (socketUtil == null) {
                    socketUtil = new SocketUtil();
                }
            }
        }
        return socketUtil;
    }
    
    private SocketUtil() {
        
    }

}

构造函数为私有的,那么其他地方就不能再生产多的实例,保证了进程中实例的唯一性;

but, 我看了几个目前安卓端github上最火的轮子Glide、EventBus的源码,发现它们并没有把构造函数私有化,总不能说大佬连最基本的单例都不懂吧,我就纳闷了,上代码为证:

Glide: 


/**
 * A singleton to present a simple static interface for building requests with
 * {@link RequestBuilder} and maintaining an {@link Engine}, {@link BitmapPool},
 * {@link com.bumptech.glide.load.engine.cache.DiskCache} and {@link MemoryCache}.
 */
public class Glide implements ComponentCallbacks2 {
  private static final String DEFAULT_DISK_CACHE_DIR = "image_manager_disk_cache";
  private static final String TAG = "Glide";
  private static volatile Glide glide;
  private static volatile boolean isInitializing;


  /**
   * Get the singleton.
   *
   * @return the singleton
   */
  @NonNull
  public static Glide get(@NonNull Context context) {
    if (glide == null) {
      synchronized (Glide.class) {
        if (glide == null) {
          checkAndInitializeGlide(context);
        }
      }
    }

    return glide;
  }

 Glide(
      @NonNull Context context,
      @NonNull Engine engine,
      @NonNull MemoryCache memoryCache,
      @NonNull BitmapPool bitmapPool,
      @NonNull ArrayPool arrayPool,
      @NonNull RequestManagerRetriever requestManagerRetriever,
      @NonNull ConnectivityMonitorFactory connectivityMonitorFactory,
      int logLevel,
      @NonNull RequestOptions defaultRequestOptions,
      @NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions) {
    this.engine = engine;
    this.bitmapPool = bitmapPool;
    this.arrayPool = arrayPool;
    this.memoryCache = memoryCache;
    this.requestManagerRetriever = requestManagerRetriever;
    ……
  }

}

EventBus:


/**
 * EventBus is a central publish/subscribe event system for Android. Events are posted ({@link #post(Object)}) to the
 * bus, which delivers it to subscribers that have a matching handler method for the event type. To receive events,
 * subscribers must register themselves to the bus using {@link #register(Object)}. Once registered, subscribers
 * receive events until {@link #unregister(Object)} is called. Event handling methods must be annotated by
 * {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter
 * (the event).
 *
 * @author Markus Junginger, greenrobot
 */
public class EventBus {

    /** Log tag, apps may override it. */
    public static String TAG = "EventBus";

    static volatile EventBus defaultInstance;

    private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();


    /** Convenience singleton for apps using a process-wide EventBus instance. */
    public static EventBus getDefault() {
        if (defaultInstance == null) {
            synchronized (EventBus.class) {
                if (defaultInstance == null) {
                    defaultInstance = new EventBus();
                }
            }
        }
        return defaultInstance;
    }


    /**
     * Creates a new EventBus instance; each instance is a separate scope in which 
       events are delivered. To use a
     * central bus, consider {@link #getDefault()}.
     */
    public EventBus() {
        this(DEFAULT_BUILDER);
    }
}

再看看jdk中的Runtime这个类的代码,构造方法私有化了,如下:

public class Runtime {
    private static Runtime currentRuntime = new Runtime();


    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() {
        return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}
}

哈哈,到底该不该私有化呢?

此问题令小弟夜不能寐,以前一直写单例都没思考过这个问题,最后请教了几个大神,我得出以下结论:

标准的单例模式,构造函数是需要私有化的,但是单例模式是一种思想,代码模式设计的目的是为了服务于业务的,可能某些特俗场景下,需要外界使用构造函数。

好了,请路过的大神多多指点迷津,不胜感激!

### ### 构造函数能否设置为私有 在面向对象编程中,构造函数可以被设置为私有。这种做法常见于需要限制类的实例化方式的设计模式中,例如单例模式。将构造函数设为私有后,外部无法直接通过`new`关键字或类似方式创建对象实例,而必须通过类提供的特定方法获取实例。 #### 在C++中 在C++中,可以通过将构造函数声明为`private`来阻止外部直接实例化对象。此时,通常会提供一个静态的公共方法(如`GetInstance()`)用于返回类的唯一实例。这种方式常用于实现单例模式[^2]。例如: ```cpp class Singleton { private: static Singleton* instance; Singleton() {} // 私有构造函数 public: static Singleton* GetInstance() { if (instance == nullptr) { instance = new Singleton(); } return instance; } }; // 初始化静态成员变量 Singleton* Singleton::instance = nullptr; ``` #### 在C#中 在C#中同样支持将构造函数设为私有,并结合内部类或静态方法来实现延迟加载的单例模式。例如,使用嵌套类来持有实例并确保线程安全是一种常见做法[^1]。示例代码如下: ```csharp public class Singleton { private Singleton() {} private class Nested { internal static readonly Singleton Instance = new Singleton(); } public static Singleton Instance { get { return Nested.Instance; } } } ``` #### 在JavaJava语言也允许构造函数私有化,以控制对象的创建过程。例如,在实现单例模式时,通常会配合静态工厂方法使用私有构造函数。此外,还可以利用枚举类型天然支持单例和线程安全的特性来简化实现[^3]。基本结构如下: ```java public class Singleton { private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ``` --- ### ### 单例模式是否应使用私有构造函数 是的,在实现单例模式时,通常应当将构造函数设为私有。这样做的主要目的是防止外部代码绕过单例机制自行创建新的实例,从而破坏单例的唯一性。无论是哪种编程语言,只要支持访问控制机制,都可以通过私有构造函数来强化单例的一致性和安全性。 当构造函数私有化之后,客户端只能通过预定义的静态方法访问实例,这不仅有助于维护对象状态的一致性,还能保证在整个应用程序生命周期内仅存在一个该类的对象副本。同时,这也为后续可能引入的懒加载、线程同步等高级行为提供了基础架构支持。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值