A Mutable Singleton

A Mutable Singleton

The Singleton pattern ensures that only one instance of an object is instantiated at run time and that this instance is accessible from a well-known access point. You can change the “singular” property of a class at run time. For example, the instantiated object is a Singleton when the application is started, but you can convert it to a non-Singleton at run time.

Consider an application that can run either on a single server only in one mode and on a client and a server in another mode. You can set this mode at run time. In the server mode, you only want one instance of the application running. An example of this would be a logger that logs the server at run time. However, at some stage, you may want it to start logging client activity without shutting the application down. In this case, you want the logger to morph to a non-Singleton at run time when desired.

The following code shows an implementation of The MutableSingleton:

 1.    private class MutableSingleton {2.  3.      boolean singletonMode_ = true;4.      private static MutableSingleton self_ = null;5. 6.      private MutableSingleton () {7.       // Construct the object8.      }9. 10.   public static MutableSingleton instance () {11.	  // if we're NOT in the "singleton" mode, 12. 	  // just create a new instance every time13.	  // this method is called14.     if (! isSingleton() ) {15.       return new MutableSingleton();16.     }17. 18.	  // if we ARE in the "singleton" mode,19.	  // check to see whether our single instance has20.	  // already been created.21.     if (self_ == null) {22.       self_ = new MutableSingleton();23.     }24.     return self_;25.   }26.   27.	/**28.	 * Allows to set the mode in which this class operates -29.	 * singleton or not-singleton.30.	 * If true is passed in, the class behaves31.	 * like singleton, i.e. every time the instance()32.	 * method is called, the same instance is returned.33.	 * If false is passed in, the class behaves34.	 * just like regular class with factory method, i.e.35.	 * every time the instance() method is called,36.	 * new instance is created and returned.37.	 * The default mode is singleton.38.	 */39.   public void setSingleton (boolean singletonMode) {40.     singletonMode_ = singletonMode;41.   }42.43.	public boolean isSingleton() {44.	  return singletonMode_;45.	}46. 47. }

This class enhances a standard Singleton implementation by providing and using a boolean variable called isSingleton that indicates whether the class is a Singleton. The variable is declared on line 3 and is initialized to false. It is used on lines 11-12 to always create a new object if it is not set (i.e., the class is not in Singleton mode). Lines 21-24 provide the setter method for this variable.

See also  Comparing different methods of testing your Infrastructure-as-Code

This class may be used as follows:

 1.     // Get a MutableSingleton object.2.     MutableSingleton aSingleton = MutableSingleton.instance();3. 4.     // Get another reference to the SAME MutableSingleton object5.     MutableSingleton anotherSingleton = MutableSingleton.instance();6.  7.     // Reset the Singleton property8.     MutableSingleton.setSingleton(false);9.  10.  // Now get a new object11.  MutableSingleton notASingleton = MutableSingleton.instance();12. 

Note that the first two instantiations on lines 4-8 give references (aSingleton and anotherSingleton) to the same object, whereas the instantiation on line 11 gives a reference (notASingleton) to a new instance of the Singleton class. This is because on line 8, the Singleton property of the class is set to false. This causes lines 11-12 to be executed in the earlier code excerpt and a new MutableSingleton object is created.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist