0% found this document useful (0 votes)
5 views8 pages

Singleton Design

The Singleton Design Pattern ensures only one instance of a class exists and provides a global access point to it, commonly used for managing shared resources like logging and database connections. It discusses various implementations, including lazy initialization, eager initialization, and the Bill Pugh Singleton method, along with their pros and cons. Additionally, it addresses potential issues such as reflection attacks and provides best practices for implementing Singleton safely.

Uploaded by

Parvez Alam I
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)
5 views8 pages

Singleton Design

The Singleton Design Pattern ensures only one instance of a class exists and provides a global access point to it, commonly used for managing shared resources like logging and database connections. It discusses various implementations, including lazy initialization, eager initialization, and the Bill Pugh Singleton method, along with their pros and cons. Additionally, it addresses potential issues such as reflection attacks and provides best practices for implementing Singleton safely.

Uploaded by

Parvez Alam I
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/ 8

DESIGN PATTERN : SINGLETON

SHUBHRA MUKHERJEE 1
DESIGN PATTERN : SINGLETON

1. What is the Singleton Design Pa ern?


Defini on:
Ensures that only one instance of a class is created throughout the
applica on and provides a global access point to that instance.

2. Why use Singleton Pa ern?


To manage shared resources like:

 Logging

 Database connec ons

 Configura on se ngs

 Caching
... with single point of control.

3. How to implement a basic Singleton in Java?

4. What is Lazy Ini aliza on in Singleton?


Crea ng the instance only when it is first needed.

SHUBHRA MUKHERJEE 2
DESIGN PATTERN : SINGLETON

5. Is Lazy Singleton Thread Safe?

No, mul ple threads can create mul ple instances.


Fix: Add synchronized.

6. How to make Singleton Thread-Safe?

7. What is Double-Checked Locking?

Thread-safe
Lazy
Be er performance

SHUBHRA MUKHERJEE 3
DESIGN PATTERN : SINGLETON

8. What is Eager Ini aliza on?

Thread-safe
Instance created even if not used

9. What is Bill Pugh Singleton?

Best approach
Lazy
Thread-safe
No synchroniza on overhead

SHUBHRA MUKHERJEE 4
DESIGN PATTERN : SINGLETON

10. Pros of Singleton

Controlled access to a single instance


Saves memory (shared object)
Ideal for shared resources
Global access point

11. Cons of Singleton

Hinders unit tes ng (global state)


Can become God object
Hidden dependencies
Mul threading issues if not handled properly

12. Where is Singleton used in real life?


 Logging services
 Caching
 Configura on reader
 Driver manager in JDBC
 Web drivers in Selenium

What is a Reflec on A ack?

In Java, Reflec on is a powerful API that allows run me access and


modifica on of classes, methods, and fields—even private ones.

A Reflec on A ack occurs when someone uses this capability to:


 Bypass private constructors, and
 Create a new instance of a class—even if it’s a Singleton.

SHUBHRA MUKHERJEE 5
DESIGN PATTERN : SINGLETON

Why is this a Problem for Singleton?


In Singleton pa ern:
 We rely on the private constructor to prevent crea ng mul ple objects.
 But reflec on can break this and instan ate a new object, viola ng the
"only one instance" rule.

Vulnerable Singleton (Can Be Broken with Reflec on)

SHUBHRA MUKHERJEE 6
DESIGN PATTERN : SINGLETON

Safe Singleton (Defends against Reflec on)

13. How to prevent Singleton from Reflec on a ack?

14. When NOT to use Singleton?


 In unit test-heavy code
 When global state causes bugs
 When object is not shared

15. Best Prac ces for Singleton

Use Enum Singleton if you want safety against serializa on/reflec on


Use Bill Pugh for performance and clarity
Keep constructor private
Add safeguards for Reflec on & Serializa on

SHUBHRA MUKHERJEE 7
DESIGN PATTERN : SINGLETON

16. Singleton with Parameters?

Not recommended
Singleton = one instance = no parameters
Workaround: configure via method, not constructor

20. Final Recommenda on

Thread
Strategy Lazy Easy Perf
Safe

Eager Ini aliza on

Synchronized Lazy

Double-Checked Lock

Bill Pugh

Enum Singleton

Recommended: Bill Pugh or Enum Singleton for produc on.

SHUBHRA MUKHERJEE 8

You might also like