简体   繁体   English

通过静态方法访问单例字段

[英]Access singleton's fields via a static method

I have a singleton class. 我有单身人士班。

When accessing the methods of the class I have the choice of two possibilities. 访问类的方法时,我有两种选择。

  1. Create those methods as instance specific and then get the instance and invoke them 创建特定于实例的那些方法,然后获取实例并调用它们
  2. Create those methods as static and invoke them and they will get the instance 将这些方法创建为静态方法并调用它们,它们将获取实例

For example: 例如:

Class Test{

 private int field1;

 Test instance;

 private Test(){};

 private Test getInstance(){
    if (instance == null)
       instance = new Test();
    return instance;
 }

 public int method1() { return field1;}
 public static int method2() {return getInstance().field1;}
}

Now, elsewhere I can write 现在,在其他地方我可以写

 int x = Test.getInstance().method1();
 int y = Test.method2();

Which is better? 哪个更好? I can think of a 3rd alternative where I use "instance" directly in the static method and then capture the exception if it is null and instantiate it and then re-invoke itself. 我可以想到第三种替代方法,在该方法中,我直接在静态方法中使用“实例”,然后捕获异常(如果该异常为null)并将其实例化,然后重新调用自身。

I could, in theory, just make the whole lot static. 从理论上讲,我可以使整个过程保持静态。 However, this will create me problems when saving the state at activity close since the serialization doesn't save static. 但是,这会在活动关闭时保存状态时给我带来问题,因为序列化不会保存静态内容。

You should avoid making everything static. 您应该避免使所有内容变为静态。 Some people would even say that a singleton is not done. 甚至有人会说单身未完成。

I think the first one is cleaner. 我认为第一个是清洁工。

However, keep in mind that under some extreme cases, Android may kill your static instances. 但是,请记住,在某些极端情况下,Android可能会杀死您的静态实例。 See this for example: http://code.google.com/p/acra/ . 例如,请参见: http : //code.google.com/p/acra/

A workaround I've found somewhere for this, is to keep a reference to your singleton from the Application class, as well. 我为此找到的一种解决方法是,也保留对Application类的单例的引用。 I don't know how problem-proof this is, though. 不过,我不知道这是如何解决问题的。

The whole point of the singleton pattern is that you can change the implementation . 单例模式的全部要点是您可以更改实现 In most cases you use it to keep the possibility open to "hook" in some other implementations of this functionality later. 在大多数情况下,您可以使用它来使以后在此功能的某些其他实现中容易被“钩住”。

Read: when deciding in favor of singleton plan for a setInstance method too, not just for a getInstance . 阅读:当也支持setInstance方法的单例计划时,不仅限于getInstance - If this does not make sense, just use a plain static class. -如果没有意义,请使用纯静态类。

In the other hand singletons are out of season, if you want to be hip and all that. 另一方面,如果您想变得时髦,那么单身人士已经过时了。 Do a search for " eliminating global state ". 搜索“ 消除全局状态 ”。 There are some Google-sponsored talks about it too. 也有一些由Google赞助的讨论。 In short: your code will be more testable and helps you avoid some dependency chaos. 简而言之:您的代码将更具可测试性,并可以帮助您避免一些依赖混乱。 (Besides being hip and all, it is definitely a step into the right direction). (除了保持臀部外,绝对是朝正确方向迈出的一步)。

In my personal opinion having static methods is bad design in the first place. 我个人认为,使用静态方法首先是不好的设计。 It, of course, depends on the program itself, but allowing a class to have static method will have impact on the whole design. 当然,它取决于程序本身,但是允许类具有静态方法将对整个设计产生影响。 Some reasoning behind my statement: 我的陈述背后的一些原因:

  1. If static method can easily change state of some object, sooner or later bugs will emerge 如果静态方法可以轻松更改某些对象的状态,则迟早会出现错误。
  2. If you publish static method with your program, every client that will use it will have a very strong dependency on your code. 如果您在程序中发布静态方法,则将使用该方法的每个客户端将对您的代码具有非常强的依赖性。 If you decide to remove or change this method someday - you will break every single client that used your class. 如果您决定有一天删除或更改此方法-您将破坏使用该类的每个客户端。

So, if you can - avoid it . 所以, 如果可以的话-避免它

If, from any reason, you will insist on having static method, I guess the first solution is better . 如果出于任何原因您将坚持使用静态方法,那么我认为第一个解决方案会更好 That's how singleton should work. 那就是单例应该工作的方式。 You should obtain a reference to a SINGLETON OBJECT via static method, but this object should be then used according to all principles from Object Oriented Programming . 您应该通过静态方法获得对SINGLETON OBJECT的引用,但是应根据面向对象编程的所有原则使用该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM