You are on page 1of 6

Singleton Design Pattern

Presented By Presented To
Name : Md.Obaidur Rahman Asif Khan Shakir
ID : 183-35-2584 Senior Lecturer
Department Of SWE Department Of SWE
Daffodil International University Daffodil International
University
Introduction

The singleton pattern is one of the simplest


design patterns. Sometimes we need to have only
one instance of our class for example a single DB
connection shared by multiple objects as creating
a separate DB connection for every object may be
costly. Similarly, there can be a single
configuration manager or error manager in an
application that handles all problems instead of
creating multiple managers.
Definition

The singleton pattern is a design pattern that restricts


the instantiation of a class to one object. 
Let’s see various design options for implementing
such a class. If we have a good handle on static class
variables and access modifiers this should not be a
difficult task.
Implement
We're going to create a singleobject class. SingleObject class have its constructor as private and
have a static instance of itself.SingleObject class provides a static method to get its static instance
to outside world. SingletonPatternDemo, our demo class will use SingleObject class to get a 
SingleObject object.
Advantage

1.While using multi-threading, to manage the multi-thread


Pool.
2.to manage the "service host repositories" in SOA
(service oriented architecture).
3.for Logging Framework implementation
4.in automation Testing/Unit Testing project i.e. Coded UI
projects.
5.While implementing the Caching in a big application.
6.for configuration settings to make proper control over
the application.
Disadvantage

1. There can be only one instance which is though the single


most feature of this pattern, it is also an inhibiting feature
when more than one instance is required. For example, if
more than one one logger is needed and logging mechanism
is implemented through singleton.
2. Serialization of instantiation is required in-order to make
singleton class thread-safe.
3. Global access to singleton object is a bad design practice.
4. Singleton does not support inheritene of the class.

You might also like