CHAPTER 25

image

Singleton

Singleton Defined

Singleton is a design pattern where you can have only one instance of a class. Usually, when you define a class, you expect to use many instances of the class. But in some designs this doesn’t make sense.

For instance, an application may only need one reference to the file system (since there is only one file system). Or the app has a data model that should stay in sync and so you want to make sure you have only one instance of a class available.

To implement a Singleton pattern, you will need to create a special type of constructor and then only use this constructor to get a reference to the Singleton object. ...

Get Objective-C Quick Syntax Reference 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.