You are on page 1of 15

Design

Design Patterns
Patterns
CE00362-3
CT070-3-3

Creational Pattern
Introduction & Overview
Singleton

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)


Design Pattern -Template

Name: A Pattern should be given an appropriate name that


describes the concept described by the pattern.

Problem: This is the statement that describe the intent of the


patent. It identifies and describes the objective that the pattern
seeks to achieve within specified pre-condition and constraints.

Context: It refers to the precondition under which the problem


seems to occur repeatedly.

Forces: Constraints addressed by the solution.

Solution :Solution must resolve all the forces in the given


context. Singleton Slide 1 (of 18)
CT070-3-3 - Design Patterns
Singleton
Name: Singleton
Problem :
How can a class be designated such that it must
compulsorily have one instance and the identifier of
the instance should be accessed globally with in the
application
Context: 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

Forces:
One solution to the above task is to make the
object a global variable, but that is not a
good solution since it violates encapsulation

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


Definition - Singleton

Solution :

Create a class with private constructor and static


method to create instance for the class.

When the class is first accessed ,an object is


created ,its identifier is stored in a class variable.

If the class is subsequently accessed ,no other


object is created. But the identifier of the object
created during first invocation is returned.

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


Singleton -Description

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.
Classified under creational pattern since it
creates object.

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


Singleton Example

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


How to Instantiate?

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


Company.java (Contd)

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


Main program to test the
singleton

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


Result-Singleton

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


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

Mahesh.P.Matha ,Obejct Oriented Analysis


and Design,Eastern Economy Edition.

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

You might also like