You are on page 1of 2

Software Design and Architecture

Q1. Singleton design pattern provides stricter control over global variables.

 Justify the statement with your own logic.


Ans. The Singleton Design Pattern allows you to ensure that a class has only one
single instance while providing a global access to this instance. This is useful if
only one object is needed. The Singleton pattern allows you to access an object
from anywhere in the program. It also ensures that the instance is not
overwritten by any other code.

 Compare Singleton design pattern and global variable (similarities and


differences)
Ans. Differences

Singleton Design Pattern Global Variable


Singleton pattern ensures that the Global variables are lexically scoped.
class has one instance and that is also
globally accessible.
Singleton design pattern is either A global variable doesn’t allow you to
created upon first access or program instantiate an object only when it is
initialization. Singletons are created required. The object is there the whole
only once during runtime time.
A Singleton pattern can be accessible Global variables are declared above all
via a global, static instance field since the functions, so all the functions can
Singleton is static. manipulate them during run time.
Singletons are used to create an Global variables don’t provide
instance of a class. encapsulation.
Singleton is not freed until the Global variables are freed after use.
termination of the program.
Similarities
Both Singleton and Global Variables are globally accessible.

 Implement any example (in Java), representing concept of global variable


and Singleton design pattern, highlight the differences with coding and
output.

You might also like