Good case for Singletons?

后端 未结 10 1667
臣服心动
臣服心动 2021-01-05 07:24

I have an application that has several classes used for storing application-wide settings (locations of resources, user settings, and such). Right now these classes are just

10条回答
  •  遥遥无期
    2021-01-05 07:56

    Effective Java says:

    Singletons typically represent some system component that is intrinsically 
    unique, such as a video display or file system.
    

    So if your component warrants single instance accross the entire application and it has some state, it makes sense to make it a singleton

    In your case, the settings of the application is a good candidate for singleton.

    On the other hand, a class can only have static methods if you want to group certain functions together, such as utility classes, examples in jdk are java.util.Arrays java.util.Collections. These have several related methods that act on arrays or collections

提交回复
热议问题