Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Singleton instance in Java

I have a Singleton class to save the state of an application's module. This class simply have a lot of class variables with setters and getters :

public class ModuleState{

private static ModuleState instance;

private A a;
private B b;
private C c;
..
..
..
..

private ModuleState (){}

public ModuleState getInstance(){
    if(instance==null)
        instance=new ModuleState();

    return instance;
}

}

At a precise moment of the application lifecycle, i have the need to CLEAR the module's state. What i do now is to reset ALL the variables in ModuleState by a clearAll() method like this:

public void clearAll(){
    a=null;
    b=null;
    c=null;

    ..
    ..
}

My question is the following : there is a cleaner method to do this reset? Possibly clearing the singleton instance itself, without resetting every class variable?

The problem with this approach is that i may have the need to add a new class variable to the ModuleState. In this case i must remember to add a line in the clearAll() method to reset the new variable.

like image 607
Nameless Avatar asked May 12 '13 09:05

Nameless


People also ask

How do I delete a singleton instance?

And deletion of singleton class object would be allow only when the count is zero. To design C++ delete singleton instance, first, we need to make the singleton class destructor private, so, it can not be accessed from outside of the class. Hence, user cannot delete the singleton instance using the keyword “delete”.

How do you destroy a singleton instance in Java?

There is no way to destroy a Singleton without breaking the Singleton property. As others have said, maybe a Singleton isn't appropriate here. If the number of instances of the class can go down to zero, then it's not a Singleton.

How do you destroy the singleton pattern?

Serialization is used to convert an object of byte stream and save in a file or send over a network. Suppose you serialize an object of a singleton class. Then if you de-serialize that object it will create a new instance and hence break the singleton pattern.

What does the method getInstance () do in the singleton?

The getInstance method is Singleton's gatekeeper. It returns the one and only instance of the object while maintaining a private reference to it which is not accessible to the outside world.


2 Answers

What about ...

public static volatile ModuleState instance = null;

public static void reset() {
    instance = new ModuleState();
}

p.s.: as per discussion below: in a multithreaded environment it's very important to synchronize the access on the instance because the JVM is allowed to cache its value. You can use volatile as shown above. Thanks to all!

Cheers!

like image 137
Trinimon Avatar answered Nov 08 '22 17:11

Trinimon


no, this approach is perfectly acceptable. you are of course synchronizing access to these state objects in some way, right? otherwise you risk someone seeing a half-cleared config object.

another thing you could do to future-proof yourself against any extra state added in the future is store all of your state in a HashMap, for example, instead of individual fields. this way, clear()ing the hashmap ensures that all state is wiped and adding any extra state in the future becomes safer

like image 43
radai Avatar answered Nov 08 '22 19:11

radai