Java: Lazy Initializing Singleton

前端 未结 5 686
情书的邮戳
情书的邮戳 2020-12-10 16:14

The pattern to create singletons seems to be something like:

public class Singleton {
    private static final Singleton instance = new Singleton();
    priv         


        
5条回答
  •  没有蜡笔的小新
    2020-12-10 16:46

    How about you lazy initialize in the build phase where you execute the unit tests. Then you change the code back to inline initialize before it's compiled for distribution.

    Your production code is inline initialized, except during your tests. Perhaps this discrepancy btw production and testing code could introdude bugs, but which?

    (Of course if this is a solution, we let a build phase + tool do the work. I see this facilitated with maven and dp4j).

提交回复
热议问题