Chapter 21. The Singleton Pattern and the Double-Checked Locking Pattern

Overview

This chapter continues the e-commerce case study introduced in Chapter 9, “The Strategy Pattern.”

This chapter

•   Illustrates how a factory can control the number of instances of an object that can ever exist, using the Singleton pattern.

•   Describes the key features of the Singleton pattern.

•   Introduces a variant to the Singleton called the Double-Checked Locking pattern.

•   Describes some of my experiences using the Singleton pattern in practice.

The Singleton pattern and the Double-Checked Locking pattern are very simple and very common. Both are used to ensure that only one object of a particular class is instantiated. The distinction ...

Get Design Patterns Explained: A New Perspective on Object-Oriented Design, Second Edition 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.