DESIGN PATTERNS


Singleton Pattern in Swift

May 8, 2019

Author: Jean-Marc Boullianne

"The Singleton pattern ensures a class only has one instance, and provides a global point of access to it"1

In many cases it is best for only one instance of a class to be instantiated at a time. A common use would be for logging, or for a class which holds the data model for your program. In many cases, having multiple instances of the object would complicate update operations or make it harder to ensure data was in sync between all instances. The best part is that your Singleton class can do all the work of making sure only one instance has been created of itself. An example is given below in Swift.

1 “Singleton.” Design Patterns Elements of Reusable Object-Oriented Software, by Erich Gamma et al., Addison-Wesley Pearson Education, 1995, pp. 127–134.