0% found this document useful (0 votes)
62 views13 pages

SADP Active Learning

This document discusses the singleton design pattern, which ensures that only one instance of a class is created. It provides the issues that can arise with the singleton pattern, such as subclasses being able to break the singleton property. The issues covered are subclasses implicitly calling the parent constructor; the constructor not being protected; thread safety issues if the singleton instance variable is not set atomically; multiple classloaders allowing multiple instances; and serialization/deserialization allowing multiple instances to be created. Examples of applications of the singleton pattern include hardware interfaces, logging, configuration files, and caching.

Uploaded by

Shane Mak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views13 pages

SADP Active Learning

This document discusses the singleton design pattern, which ensures that only one instance of a class is created. It provides the issues that can arise with the singleton pattern, such as subclasses being able to break the singleton property. The issues covered are subclasses implicitly calling the parent constructor; the constructor not being protected; thread safety issues if the singleton instance variable is not set atomically; multiple classloaders allowing multiple instances; and serialization/deserialization allowing multiple instances to be created. Examples of applications of the singleton pattern include hardware interfaces, logging, configuration files, and caching.

Uploaded by

Shane Mak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

SADP Active Learning

The Issues in Singleton Design Pattern

Mohan Madame
Mandar Kulkarni
Yogesh Khubchandani
ABSTRACT
● Design Patterns are standard solutions to problems in object-oriented
software engineering has proven fruitful in the software design unit.
● These patterns guarantee better quality software and at the same time a
novice designer feels at home when it comes to understanding the system
functionality.
● There are many design patterns that solves specific software design
problems. One of which is the Singleton Pattern which deals with the
object creation mechanism.This paper highlights the various issues in this
pattern
What are design patterns
● Design Patterns are a way of implementing a common solution to a
common problem in object-oriented software.
● They describe the proven solutions to recurring problems.
● They are categorized into :
1. Creational Patterns
2. Behavioral Patterns
3. Structural Patterns
What is singleton design pattern

● The Singleton Pattern which is a class of creational design pattern


deals with ways to create instances of classes.
● The best practice is to abstract the object initialization process into a
class.
● The Singleton Pattern assures that there is one and only one
instance of a class created, and provides a global point of access to
it.
What is singleton design pattern

1. Usually the singleton class


contains getInstance() method
to return instance of class if
exists else it creates instance
and then returns it.
2. It has private constructor so
that object can’t be created
outside the class
3. Singleton class is final so that
it can’t be made a superclass.
Issues in Singleton design pattern
● Consider a case where a class extends singleton class. As
shown below in the code snippet, here the constructor is
called implicitly through super().

Issue 1
● The constructor is not protected.
● If the constructor is protected or public then a class, which is
in the same package, can create object of Singleton class
Issue 2 which violates the Singleton criteria.
● Hence it is usually private in nature.
● If a thread is preempted at Line 2 before the assignment is
made, the instance member variable will still be null, and
another thread can subsequently enter the if block.
● In that case, two distinct singleton instances will be created.

Issue 3
● Because multiple classloaders are commonly used in many
situations—including servlet containers—we can end up with
multiple singleton instances

Issue 4
● If we serialize a Singleton and then deserialize it twice, there
will be two instances of the singleton
● Solution is to implement the readResolve () method.

Issue 5
Applications

● Hardware interface access


○ Print spooler can be made singleton.
● Logger
○ Create a global point of reference for all clients.
● Configuration files
● Cache
Thank You !

You might also like