© Vaskaran Sarcar 2020
V. SarcarDesign Patterns in C#https://doi.org/10.1007/978-1-4842-6062-3_1

1. Singleton Pattern

Vaskaran Sarcar1 
(1)
Garia, Kolkata, West Bengal, India
 

This chapter covers the Singleton pattern.

GoF Definition

Ensure a class has only one instance, and provide a global point of access to it.

Concept

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

But let’s take a closer look. If you use 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 (particularly when constructor calls are truly expensive), so you need to restrict ...

Get Design Patterns in C#: A Hands-on Guide 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.