LECTURE NO 7
Singleton
pattern
What is the Singleton Pattern?
The Singleton pattern ensures that a class is only
instantiated once and provides a global access point for
this instance
Creational Pattern
Abstracts the instantiation process
Object Creational Pattern
Delegates the instantiation to another
object
Why use the Singleton Pattern?
It is important for some classes to only have one instance
It is also important that this single instance is easily
accessible
Why not use a global object?
Global variables make objects
accessible, but they don't prevent the
instantiation of multiple objects.
Solution
Make the class responsible for keeping track of its only
instance
The class can ensure that no other instances are created
This provides a useful way to access the instance
This is the Singleton pattern
indicate that only one instance will be created (a
The Singleton Pattern
singleton)
1
ServicesFactory
UML notation: in a instance : ServicesFactory singleton static
class box, an attribute
underlined attribute or accountingAdapter : IAccountingAdapter
method indicates a inventoryAdapter : IInventoryAdapter
static (class level) taxCalculatorAdapter : ITaxCalculatorAdapter
member, rather than singleton
an instance member getInstance() : ServicesFactory static
method
getAccountingAdapter() : IAccountingAdapter
getInventoryAdapter() : IInventoryAdapter
getTaxCalculatorAdapter() : ITaxCalculatorAdapter
...
// static method
public static synchronized ServicesFactory getInstance() • Issues with the factory
{
if ( instance == null ) pattern:
instance = new ServicesFactory()
return instance – Who creates the factory
} itself?
– How do we get access to
the factory class from
everywhere?
Accessing the Singleton
1
:Register
:ServicesFactory
initialize
aa = getAccountingAdapter
the ‘1’ indicates that visibility
to this instance was achieved
...
via the Singleton pattern
public class Register {
public void initialize() {
...
aa = ServicesFactory.getInstance().getAccountingAdapter();
...
}
}
The Singleton Pattern: Implementation and Design
Issues
Lazy initialization:
public static synchronized ServicesFactory getInstance() {
if (instance == null)
instance = new ServicesFactory();
return instance;
}
Eager initialization: ???
The Singleton Pattern: Implementation and Design
Issues
Lazy initialization:
public static synchronized ServicesFactory getInstance() {
if (instance == null)
instance = new ServicesFactory();
return instance;
}
Eager initialization:
public class ServicesFactory {
private static ServicesFactory instance =
new ServicesFactory();
public static ServicesFactory getInstance() {
return instance;
}
}
Singleton Issues
Lazy vs. eager initialization. Which one is better?
Laziness, of course!
Creation work (possibly holding on to expensive
resources) avoided if instance never created
Why not make all the service methods static
methods of the class itself?
Why do we need an instance of the factory object
and then call its instance methods?
Singleton Issues
Why not make all the service methods static
methods of the class itself?
To permit subclassing: Static methods are not
polymorphic, don’t permit overriding.
Object-oriented remote communication
mechanisms (e.g. Java RMI) only work with instance
methods
Static methods are not remote-enabled.
More flexibility: Maybe we’ll change our minds and
won’t want a singleton any more.