我在自由起诉辛格尔顿模式,我在想;什么时候够了,够了?这些都是我现在的单身人士。我并没有像所有人一样使用它们。我过度使用Singleton模式吗?
Database db = Database.getInstance(this); // wrapper for the library
Realm realm = Realm.getInstance(this); // library I use
AppUtils appUtils = AppUtils.getInstance(this); // app specific functions that manipulate database queries to produce targeted values
Utils utils = Utils.getInstance();
Generator gen = Generator.getInstance(this); // necessary for the library
我想也做这样的事情
AppFragments fr = AppFragments.getInstance(this, AttributeCommonInEveryActivity>)
或者我应该接口,并做BlerpActivity extends BlourpActivity
最后,以清除可能发生的影响你的意见的任何偏差。我知道,似乎我只使用一种设计模式,但我不是。我在考虑设计和实用方面工作^ _^
如果'Database db'的确是“库的包装器”,那么你不需要'Realm realm',因为'db'应该包含'Realm'实例,并且如果有的话可以通过'db'获取它除了'db'需要它。关于你的其他三个单身人士,不知道他们做了什么,很难告诉你。尽管如此,为什么数据需要在全球范围内成为具体的具体原因。单身人士因为“打字很难”不是一个好的理由。 :-) – CommonsWare
似乎你可以使用一个好的[DI](https://en.wikipedia.org/wiki/Dependency_injection)框架,例如[Guice](https://github.com/google/guice/wiki /动机)。这样,通过简单地[范围](https://github.com/google/guice/wiki/Scopes)适当地创建对象,就可以完全避免手写的JVM单例(一种反模式)。 –
@CommonsWare 您好,非常感谢您的反馈。领域是图书馆,而不是我自己的建筑。这就是你与数据库沟通的方式(叫Realm/lame)。 这是我无法避免的唯一单例。数据库获取上下文并创建一个新的RealmInstance来获取常用结果。 你绝对正确的最后一句话。我尽量使它尽可能模块化 – MayTheSchwartzBeWithYou