EN VI

JAVA - Implementing a serialisable and mutable Singleton?

2024-03-14 04:30:05
How to JAVA - Implementing a serialisable and mutable Singleton

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;
    }
}

Solution:

A singleton does not have to be immutable: all that the singleton pattern asserts is that there only exists one of this object type within the whole application. Other objects that want to access an idCountSingleton can only retrieve it using the getInstance() method, and cannot create it themselves.

To finish making the class singleton:

  1. Ensure the getInstance() method is also static, so that other classes can access it by the class name (like idCountSingleton.getInstance())
  2. I'd highly recommend following this pattern within the getInstance() method:
public static synchronized idCountSingleton getInstance() {
    if(INSTANCE == null) {
        INSTANCE = new ClassSingleton();
    }
    return INSTANCE;
}

The if-null check lazily constructs the object, waiting for getInstance() to be called before creating the singleton object. The synchronized keyword in the method signature ensures that only one thread at a time can enter the method to additionally handle race conditions (e.g., what happens if two threads make it past the if-null check before either creates the object?)

Serializing the object (presumably, on shutdown/autosave) can then be handled by accessing the object with getInstance() from whatever class saves the application state, and deserialization can happen within the private constructor (assuming that this is a singleton object, you can save its data to a fixed location)

To note: the only thing you need to serialize is the count field. static fields are functionally transient.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login