© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
V. SarcarJava Design Patternshttps://doi.org/10.1007/978-1-4842-7971-7_7

7. Singleton Pattern

Vaskaran Sarcar1  
(1)
Garia, Kolkata, India
 

This chapter covers the Singleton pattern.

GoF Definition

It ensures that a class has only one instance and provides a global point of access to it.

Concept

Let’s assume you have a class called A and you need to create an object from it. Normally what do you do? You guess it right; you simply use the following line of code: A obA = new A();

But let’s look at it closely. If you keep using the new keyword ten more times, you’ll have ten more objects. Right? But in a real-world scenario, unnecessary object creation is a big concern ...

Get Java Design Patterns: A Hands-On Experience with Real-World Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.