简体   繁体   English

实现单例模式而不在Java中使用私有引用?

[英]Implement singleton pattern without using a private reference in java?

This was asked in a recent discussion, but was not able to tackle it properly. 在最近的讨论中,有人问过这个问题,但未能正确解决。 However I answered it by giving example of Enum but he was looking for some other way. 但是我以举Enum为例来回答,但他正在寻找其他方式。 Can you highlight the ways for which we can overcome above question? 您能否强调我们克服上述问题的方式?

This is how you would implement a Singleton with a public field : 这是在public场所实施Singleton的方式:

public class Singleton {
      public static final Singleton INSTANCE = new Singleton();

      private Singleton() {}

}

Further reading on the pros and cons of this approach : 进一步阅读此方法的优缺点:

Item 3 from Joshua Bloch's Effective Java. 约书亚·布洛赫(Joshua Bloch)的《有效的Java》 第3条

Short answer? 简短的答案?
Using a private YourClass constructor and a public YourClass variable with eager instantiation 使用private YourClass构造函数和带有急切实例化的public YourClass变量

Long answer 长答案
https://en.wikipedia.org/wiki/Singleton_pattern https://zh.wikipedia.org/wiki/Singleton_pattern

public class Singleton {
      public static final Singleton INSTANCE = new Singleton();
    //^ public variable

      private Singleton() {}
    //^ private constructor
}

It is a bad idea, but if you change the private variable to a public one, you will achieve your goal. 这是一个坏主意,但是如果将private变量更改为public变量,则可以实现目标。 You probably should declare the variable as final as well to avoid surprises, but it is not strictly necessary, and doesn't make it a good idea (IMO). 您可能还应该将变量声明为final ,以避免出现意外情况,但这并不是绝对必要的,并且也不是一个好主意(IMO)。

For what it is worth, there is no way to implement a singleton that doesn't involve an explicit variable to hold the instance, or an enum . 就其价值而言,无法实现不包含用于容纳实例或enum的显式变量的单例。


Here is what Bloch has to say on the tradeoff between the public and private variable approaches: 关于公共变量和私有变量方法之间的折衷,Bloch不得不说:

"One advantage of the factory-method approach is that it gives you the flexibility to change your mind about whether the class should be a singleton without changing its API. The factory method returns the sole instance but could easily be modified to return, say, a unique instance for each thread that invokes it. A second advantage, concerning generic types, is discussed in Item 27. Often neither of these advantages is relevant, and the final-field approach is simpler." “工厂方法的一个优势是,它使您可以灵活地决定是否应该在不更改类的情况下改变类,而不必更改其API。工厂方法返回唯一的实例,但可以轻松地修改为返回,例如,在第27项中讨论了与泛型类型有关的第二个优点。通常,这些优点都不相关,并且最终字段方法更简单。

My argument is that knowing that you won't need to change your mind in the future involves a degree of prescience. 我的论点是, 知道将来不需要改变主意会涉及一定的先见之明。

Or to put it another way, you can't know that: you can only predict that. 或者换句话说,您可能不知道 :您只能预测这一点。 And your prediction can be wrong. 而且您的预测可能是错误的。 And if that prediction does turn out to be wrong, then you have to change every piece of code the accesses the singleton via the public variable. 而且,如果该预测确实是错误的,那么您必须更改每个代码段,以通过公共变量访问单例。

Now if your codebase is small, then the amount of code you need to change is limited. 现在,如果您的代码库很小,那么您需要更改的代码量将受到限制。 However, if your codebase is large, or if it is not all yours to change, then this this kind of of mistake can have serious consequences. 但是,如果您的代码库很大,或者如果不是所有代码都需要更改,那么这种错误可能会导致严重的后果。

That is why it is a bad idea. 这就是为什么这是一个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM