05-1 Creational Pattern - Singleton

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Design

Design Patterns
Patterns
CE00362-3
CT070-3-3

Creational Pattern – Singleton


Introduction & Overview

CT070-3-3 - Design Patterns Singleton Slide 1 (of 17)


Learning Outcomes

At the end of this session, you should be


able to;

– Describe benefits of Singleton pattern


– Recognise and apply Singleton pattern
given a scenario

CT070-3-3 - Design Patterns Singleton Slide 2 (of 17)


Singleton

Intent:
To ensure that a class has only one instance, and to
provide a global point of access to it.
Motivation:
It is important for some classes to have exactly one
instance. Although there can be many printers in a
system, there should be only one printer spooler. How
do we ensure that a class has only one instance and
that the instance is easily accessible?

CT070-3-3 - Design Patterns Singleton Slide 5 (of 17)


Description - Singleton

Applicability:
Use Singleton pattern when
- There must be exactly one instance of a class,
and it must be accessible to clients from a
well-known access point.
- When the sole instance should be extensible
by subclassing, and clients should be able to
use an extended instance without modifying
their code.

CT070-3-3 - Design Patterns Singleton Slide 6 (of 17)


Definition - Singleton

Ensures that a class has only one instance


and to provide a global point of access to it.

CT070-3-3 - Design Patterns Singleton Slide 7 (of 17)


Responsibility Pattern?

• Objects can usually act responsiblity


– Just by performing their own work
• Some objects take on further
responsibilities
– Representing real-world entities
– Coordinating work
– Modeling the state of a system.

CT070-3-3 - Design Patterns Singleton Slide 8 (of 17)


Global Access

• When a particular object in a system bears


a global responsibility
– you need a way to find the responsible object.
• In some cases the object you need will be
the only instance of its class.

CT070-3-3 - Design Patterns Singleton Slide 9 (of 17)


A Single Object

• You want to ensure that there is exactly


one instance
• And, you want to provide other developers
a way to access this instance.
• Example: Factory Class

CT070-3-3 - Design Patterns Singleton Slide 10 (of 17)


How and Why

• The mechanics of Singleton are more


memorable than its intent.
• It is easier to explain how to ensure that a
class has only one instance than it is to
say why you might want this restriction.
• You might categorize Singleton as a
“creational” pattern.

CT070-3-3 - Design Patterns Singleton Slide 11 (of 17)


When to Instantiate?

• One choice is to create this instance as a


static field in the class.

– For example, the Runtime class in java.lang


includes the line:
– private static Runtime
currentRuntime = new Runtime();

CT070-3-3 - Design Patterns Singleton Slide 12 (of 17)


Lazy Initialization
• You might wait until the instance is first
needed.
• A Factory class might make its single
instance available with:
public static Factory getFactory() {
if (factory == null) {
factory = new Factory();
// ...
}
return factory;
}

CT070-3-3 - Design Patterns Singleton Slide 13 (of 17)


Garbage Collection

• Once you create a singleton instance,


– it might get garbage-collected
• You might want to prevent this
– If your object has a significant startup cost
– Or if it is gathering statistics

CT070-3-3 - Design Patterns Singleton Slide 14 (of 17)


Summary

• Ensure that a class has only one instance,


• And provide a global point of access to it.
• Regardless of the mechanics, the value of
Singleton lies in centralizing authority in a
single object.

CT070-3-3 - Design Patterns Singleton Slide 15 (of 17)


Question and Answer Session

Q&A

CT070-3-3 - Design Patterns Singleton Slide 16 (of 17)


References

Steven John Metsker, Design Patterns Java


Workbook, Addison Wesley

Erich Gamma et. al., Design Patterns –


Elements of Reusable Object-Oriented
Software, Addison Wesley

CT070-3-3 - Design Patterns Singleton Slide 17 (of 17)

You might also like