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.

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as