How to make an immutable singleton in Java?

后端 未结 4 1788
后悔当初
后悔当初 2021-01-19 01:26

An immutable object is initialized by its constuctor only, while a singleton is instantiated by a static method. How to make an immutable singleton in Java?

4条回答
  •  爱一瞬间的悲伤
    2021-01-19 01:57

    while a singleton is instantiated by a static method

    While this is the usual way of doing it, this is by no means the only way.

    In Java 1.5 a new version of Singleton is the enum singleton pattern:

    public enum Elvis{
    
    INSTANCE // this is a singleton, no static methods involved
    
    }
    

    And since enums can have constructors, methods and fields, you can give them all the immutable state you want.

    Reference:

    • Java tutorial: Enum Types
    • Effective Java, Item 3
    • Singleton (the enum way) (WikiPedia)

    Also, the term Singleton leaves some room for interpretation. Singleton means that there is exactly one object per defined scope, but the scope can be a number of things:

    • Java VM Classloader (thanks @Paŭlo Ebermann for reminding me): in this case use enums or the initialize-through-static-inner-class pattern. This is of course what is usually meant by a singleton.
      Be Careful: enums and all other singletons are broken if loaded through multiple Classloaders.
    • Enterprise Application (in this case you need a container-managed singleton, e.g. a Spring singleton bean). This can be several objects per VM or one object per several VMs (or one Object per VM, of course)
    • Thread (use a ThreadLocal)
    • Request / Session (again, you'll need a container to manage this, Spring, Seam and several others can do that for you)
    • did I forget anything?

    All of the above can be made immutable, each in their own way (although it's usually not easy for container-managed components)

提交回复
热议问题