Applied Design Patterns with Java

Creational :: Singleton (127) {C ch 6}

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

Motivation

It's essential for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.

What makes a class have only one instance which is easily accessible? A global variable makes an object accessible, but it doesn't prevent instantiating multiple objects.

A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the
Singleton pattern.

Catalog Creational Prev Next