In: Computer Science
“Singleton design pattern provides stricter control over
global
variables.”
Justify this statement with your own logic.
Compare Singleton design pattern and global variable (similarities
and differences)
Implement any example (in JAVA), representing concept of global
variable and Singleton design
pattern, highlight the differences with coding and output.
Yes, Singleton design pattern provides stricter control over global variables. The Singleton pattern ensures that only one instance of a class will be created and provides a global point of access to it. Any global variables that are required can then be placed within this class and accessed wherever they are needed.Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.
SingleObject class provides a static method to get its static instance to outside world. SingletonPatternDemo, our demo class will use SingleObject class to get a SingleObject object.
Step 1
Create a Singleton Class.
SingleObject.java
public class SingleObject { //create an object of SingleObject private static SingleObject instance = new SingleObject(); //make the constructor private so that this class cannot be //instantiated private SingleObject(){} //Get the only object available public static SingleObject getInstance(){ return instance; } public void showMessage(){ System.out.println("Hello World!"); } }
Step 2
Get the only object from the singleton class.
SingletonPatternDemo.java
public class SingletonPatternDemo { public static void main(String[] args) { //illegal construct //Compile Time Error: The constructor SingleObject() is not visible //SingleObject object = new SingleObject(); //Get the only object available SingleObject object = SingleObject.getInstance(); //show the message object.showMessage(); } }
Step 3
Verify the output.
Hello World!
Global Variables
Now that the infrastructure of the Singleton class has been developed, the global data can be implemented.
Global data should be declared to be private and getter and setter methods should be used for access. Declaring the data as a private and controlling access ensures it will not be changed accidentally or maliciously from outside the class. It also allows the Singleton to enforce error checking and control access.
Using a standard naming convention for getters and setters will make the code much more understandable. Setters, methods that set the value of a variable, should use the prefix
set
in the method name. Getters, methods that retrieve values, should use the prefix get in the method name. Getters that retrieve a Boolean value should use the prefix
is
in the method name.
Examples:
setEnabled()getName()isEnabled()
Using this naming convention, a global integer would be implemented as seen below
// keep global data private
private int globalInteger;
public int getGlobalInteger() {
return globalInteger;
}
public void setGlobalInteger(int value) {
globalInteger = value;
}