Autumn SALE
Singleton

Singleton in Swift

Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.

Singleton has almost the same pros and cons as global variables. Although they’re super-handy, they break the modularity of your code.

You can’t just use a class that depends on a Singleton in some other context, without carrying over the Singleton to the other context. Most of the time, this limitation comes up during the creation of unit tests.

Complexity:

Popularity:

Usage examples: A lot of developers consider the Singleton pattern an antipattern. That’s why its usage is on the decline in Swift code.

Identification: Singleton can be recognized by a static creation method, which returns the same cached object.

The following examples are available on Swift Playgrounds.
Kudos to Alejandro Mohamad for creating the Playground version.

Singleton in Other Languages

Singleton in C# Singleton in C++ Singleton in Go Singleton in Java Singleton in PHP Singleton in Python Singleton in Ruby Singleton in Rust Singleton in TypeScript