I am currently developing an Object-Oriented system in java. I am required to assign a unique integer identifier for every object within the application, and to be able to serialise and deserialise the entire contents of the application. I was pointed to the Singleton design pattern as a means of storing the java equivalent of "global variables", but the common implementations in java using a Class or Enum are unserialisable or immutable respectively. How would I go about making a singleton that is both mutable and serialisable?
Below is my current java code for a Singleton class I have written which should be non-serialisable since idCountSingleton is static:
import java.io.Serializable;
public class idCountSingleton implements Serializable{
private static idCountSingleton INSTANCE;
private int count;
private idCountSingleton(int id){
idCountSingleton INSTANCE = new idCountSingleton(id);
count = id + 1;
}
public int getID(){
return this.count;
}
public idCountSingleton getInstance(){
return this.INSTANCE;
}
}