简体   繁体   中英

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.

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. 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?

Read 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. 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.

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! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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