![](https://i0.wp.com/media.dev.to/cdn-cgi/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6boxmm2g1edp891quyb.jpg?w=1200&ssl=1)
Have you ever encountered situations where you needed only one instance of a particular object to be shared across your entire application? Imagine the scenario of an office water jug – if every employee required water, they wouldn’t create a new water jug each time. Instead, they would all share the same jug, pouring water into their glasses as needed. This concept of a single, shared instance lies at the heart of singleton classes in Java.
What is a Singleton Class?
Singleton classes in Java ensure that only one instance of the class is created and provide a global point of access to that instance. This means that any part of the program can access the same instance of the class, allowing for centralized management of resources or state.
Implementing a Singleton Class: The Water Jug Analogy
Let’s translate the water jug analogy into a Java singleton class named WaterJug
:
public class WaterJug { private int waterQuantity = 500; private static WaterJug instance = null; private WaterJug() {} // Private constructor to prevent external instantiation // Method to provide the service of Giving Water. public int getWater(int quantity){ waterQuantity -= quantity; return quantity; } // Method to return the object to the user. public static WaterJug getInstance(){ // Will Create a new object if the object is not already created and return the object. if(instance == null){ instance = new WaterJug(); } return instance; }
}
Understanding the Code:
- We have a private constructor to prevent external instantiation of the class.
- The
getInstance()
method provides a way to access the single instance of the class. If the instance is not already created, it creates one and returns it. - The
getWater()
method simulates the act of taking water from the jug, reducing the water quantity.
Usage Example:
public class Office { public static void main(String[] args) { // Get the singleton instance WaterJug jug = WaterJug.getInstance(); // Employees use the same jug to get water int waterTaken = jug.getWater(100); // Employee 1 takes 100ml System.out.println("Water taken: " + waterTaken + "ml"); // Another employee gets water waterTaken = jug.getWater(50); // Employee 2 takes 50ml System.out.println("Water taken: " + waterTaken + "ml"); }
}
Thread Safety and Alternative Approaches:
While the above implementation provides a basic form of thread safety, there are more robust approaches such as using double-checked locking or Java enums for singleton classes.
Conclusion:
Singleton classes in Java offer a convenient way to ensure that only one instance of a class is created and accessed globally. By understanding the concept through real-life analogies like the office water jug scenario, you can grasp the importance and utility of singleton classes in your Java applications.
Discover more from Coursity
Subscribe to get the latest posts sent to your email.