单身人士的好例子? [英] Good case for Singletons?

查看:110
本文介绍了单身人士的好例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它有几个用于存储应用程序范围设置的类(资源位置,用户设置等)。现在这些类只是充满了静态字段和方法,但我从来没有实例化它们。

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 full of static fields and methods, but I never instantiate them.

有人建议我让他们成为单身人士,这是/反对的情况是什么?

Someone suggested I make them Singletons, What's the case for/against?

推荐答案

有效的Java说:

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.

另一方面,如果要将某些函数组合在一起,类只能有静态方法,例如实用程序类,jdk中的示例是java.util.Arrays java.util.Collections中。这些有几个相关的方法作用于数组或集合

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

这篇关于单身人士的好例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆