CSE327 Lecture10
CSE327 Lecture10
Software Engineering
AKM IQTIDAR NEWAZ [IQN]
LECTURE - 11
Singleton Design Pattern
When only one instance of a class is needed (shared resource) across your application, you
have to allow the access to that instance from the whole application but also we don’t need a
new instance every time. we only need that one instance which could be utilized across the
whole application
An instance of a class
Instance or shared resource
AM2 DB Connection AM3
Logger Instance
Singleton Design Pattern
One can argue that we can solve this problem just by saving that instance in a global variable
and allow access from different application on module / different programs in the same
application to that global variable. Let's say we do not.
We save the logger instance in the global variable. Allow access from different application
module to read that variable. It is fine as long as different application module are just reading
that variable but what if one particular module overwrites it. In that case this global variable
have changes and here is the problem.
L2
AM1 AM4
rea d global variable logger;
d rea logger = L1;
Logger rea logger = L2 (AM2)
modified that’s why we
d
rea e d can’t use global variable
AM2 writ AM3
Singleton Design Pattern
Solution: Try to initialize that variable using a private constructor and we don’t allow anyone
else to access that constructor. You only allow to access that variable by getter.
Singleton Class
One private
- Singleton : Singleton AM1 AM4
instance of rea d
Singleton class - Singleton () : void d rea
private constructor
+ getInstance () : Singleton Singleton rea
d
rea d
AM2 ite AM3
wr
public getter
return singleton
instance
Remember: Singleton class never accepts parameters. If it accepts parameters, then it becomes
a factory design pattern. So avoid it.
Eager Loading & Lazy Loading
Eager Loading Lazy Loading
The instance is already initialized as Whenever our application starts the
soon as the application is up. singleton instance is not initialized, it is
Application starts and instance is initialized only when someone invokes
already initialized. it / tries to read it.
If nobody needs it, it will be there and We don’t initialize it. When application
utilize memory. module needs it, then it creates the
instance. If its available, no need to
create it. Optimization is done.
Works well with one singleton class. Suitable for multiple singleton class
Eager Loading
Pseudocode
public class LoggerSingleton {
private static LoggerSingleton instance = new LoggerSingleton ();
private LoggerSingleton () {}; #Constructor
public static LoggerSingleton getInstance () {
return instance;
}
}
public class SingletonDemo {
public static void main (String[] args) {
LoggerSingleton singletonObj = LoggerSingleton.getInstance ();
S.out (singletonObj);
}
}
Lazy Loading
Pseudocode
public class LoggerSingleton {
private static LoggerSingleton instance = null;
private LoggerSingleton () {};
public static LoggerSingleton getInstance () {
if (instance == null) {
instance = new LoggerSingleton ();
}
return instance;
}
}