13. Singleton

The Singleton pattern is used when there must be exactly one instance of a class, and it must be easily accessible to other objects in an application. Usually, the Cocoa documentation uses the term shared instance when the Singleton pattern is in use.

In Cocoa, one example of a Singleton is the NSApplication class. It maintains the application’s connection to the window server for drawing and receiving events. Cocoa optionally takes advantage of Dynamic Creation and Bundles to substitute an instance of your own class for the default NSApplication instance.

Motivation

Define conventions for the creation of and access to an instance in cases where only one instance of a class should be created. Often singletons represent physical ...

Get Cocoa Design Patterns 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.