简体   繁体   English

单例还是静态类?

[英]Singleton or static class?

I have the following class : 我有以下课程:

public class EnteredValues {

private HashMap<String, String> mEnteredValues;

public boolean change = false;
public boolean submit = false;
private static final EnteredValues instance = new EnteredValues();

// Singleton
private EnteredValues() {
    mEnteredValues = new HashMap<String, String>();
}

public static EnteredValues getInstance() {
    return instance;
}

public void addValue(String id, String value) {

    if (mEnteredValues.put(id, value) != null) {
        // A change has happened
        change = true;
    }
}

public String getValueForIdentifier(String identifier) {
    return mEnteredValues.get(identifier);
}

public HashMap<String, String> getEnteredValues() {
    return mEnteredValues;
}

public void clean() {
    mEnteredValues.clear();
    change = false;
    submit = false;
}
}

This class is used to manage the values that a user has already entered, and the class should be accessible to all classes across the application. 该类用于管理用户已经输入的值,并且该类应可供应用程序中的所有类访问。
When the activity changes I 'reset' the singleton by calling the clear method. 当活动发生变化时,我通过调用clear方法来“重置”单例。

I chose the singleton pattern without really considering the option of a static class. 我选择单例模式时并没有真正考虑静态类的选项。
But now I was wondering if I shouldn't just use a static class.. 但是现在我想知道是否不应该只使用静态类。

What is the common way to handle a class that just manages values? 处理只管理值的类的常用方法是什么? Is a static class faster as a singleton? 静态类作为单例更快吗?

thx 谢谢

The very fact that you are providing a clear method to reset the state of your Singleton dictates that you should not use Singleton. 您提供一种清晰的方法来重置Singleton状态的事实表明,您不应使用Singleton。 This is risky behavior as the state is global. 由于国家是全球性的,因此这是冒险行为。 This also means that unit testing is going to be a big pain. 这也意味着单元测试将是一个很大的痛苦。

One more thing. 还有一件事。 Never ever declare instance variables as public. 永远不要将实例变量声明为公共变量。 Declare them as private or protected and provide getters and setters. 将它们声明为私有或受保护的,并提供获取器和设置器。 Also, there is no need to initialize instance variables with a value that is their default value. 同样,也不需要使用默认值初始化实例变量。

The main difference between a static class and the singleton pattern is that singleton may be used if you need to implement an interface or such. 静态类和单例模式之间的主要区别在于,如果您需要实现接口等,则可以使用单例。 For this particular case I think you might be better off with a static class since you are not implementing any interface. 对于这种特殊情况,我认为使用静态类可能会更好,因为您没有实现任何接口。 Relating your question if its one faster to the other, I'd say is negligible the difference but using a static class will remove a small overhead of dynamic instantiation of the class. 关于您的问题是否可以更快地解决,我想说两者之间的差异可以忽略不计,但是使用静态类将减少类的动态实例化的少量开销。

What is bad in using singleton if you need such a design? 如果需要这样的设计,使用单例有什么不好? If you need exactly one instance of some object designed to do specified things singleton is not a bad choice for sure. 如果您只需要某个对象的一个​​实例来进行指定的操作,那么单例无疑是不错的选择。

@see Are Java static calls more or less expensive than non-static calls? @see Java静态调用比非静态调用昂贵还是便宜?

Read http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html 阅读http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

From there: 从那里:

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. 注意:静态嵌套类与它的外部类(和其他类)的实例成员进行交互,就像其他任何顶级类一样。 In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. 实际上,静态嵌套类在行为上是顶级类,为了包装方便,该顶级类已嵌套在另一个顶级类中。

Just for style 只为风格

I prefer not to rely on Singleton if I don't need to. 如果不需要,我宁愿不依赖Singleton。 Why? 为什么? Less cohesion. 凝聚力较小。 If it's a property you can set from outside, then you can test your Activity (or whatever) with unit testing. 如果是可以从外部设置的属性,则可以使用单元测试来测试“活动”(或其他任何内容)。 You can change your mind to use diferent instances if you like, and so on. 如果愿意,您可以改变主意以使用不同的实例,依此类推。

My humble advise is to have a property in each of your Activities (maybe you can define a common base class?), and set it at activity initialization with a new fresh instance. 我谦虚的建议是在每个Activity中都有一个属性(也许您可以定义一个公共基类?),并使用新的新实例将其设置为Activity初始化。

Your code will not know nothing about how to get it (except the init code and maybe you can change it in the future). 您的代码对如何获取它一无所知(初始化代码除外,也许您将来可以更改它)。

But as I've said... just a matter of taste! 但是正如我所说的...只是一个口味问题! :) :)

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

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