iOS Design Pattern: Singleton Pattern

Ali Jaber
2 min readApr 21, 2021

Singleton comes from the creational design patterns family, because it’s about creating a shared instance. It adds restriction to the class to only have one instance. Each class will have only one point of access to it.

One Example of Apple usage of Singleton is the FileManager, which handles everything related to filesystem access. it has a default instance, which is a Singleton.

lets go through an example to take a deeper look into Singletons:

public class Manager {
static let sharedinstance = Manager() // declare a public static property, which is the Singleton instance
private init() {} // init is private to restrict the creational of additional instances
func printMyName(_ name: String) {
print(name)
}}// So to call the singleton:
let defaultManager = Manager.sharedinstance
//calling the printMyName function:
defaultManager.printMyName("My name")
// or we can just call the function as below:
Manager.sharedinstance.printMyName("My name")

Since the initializer is private, you aren’t able to create another instance of this Class, as below:

let defaultManager = Manager.sharedinstance 
// this is the first instance, everything is fine till now
let myNewInstanceManager = Manager.sharedinstance
// this is the second instance, here you will get a compiler error, //since you are trying to create an additional instance of Manager.

In case you want to create multiple instances of the Singleton class, just make the init as public instead of private, this type of Singleton is called Singleton plus, as below:

public class SingletonPlusManager {
static let sharedinstance = SingletonPlusManager()
public init() {}
}
let defaultManager = SingletonPlusManager.sharedinstance // first instance created
let mySecondManagerInstance = SingletonPlusManager.sharedinstance // second instance created with no compile errors

When to use Singleton and Singleton Plus

Use the singleton pattern when creating more than one instance of a specific class will cause problems, or when its not logical.

Use the singleton plus pattern if you want to allow custom instances to be created, and if the shared instance is used most of the time.

An example of Singleton plus used by Apple is the UserDefaults.standard, which is used to store key-value data.

What to be aware of:

Don’t use Singletons to pass data between views, just pass the data through a property or an initializer.

When you are sure that you need a singleton, you have to consider if you need a singleton or a singleton plus, it depends if having more than 1 instance will cause problems or not.

Singletons can make unit testing difficult, since the state is been stored in a global object, so it can be hard to mock them.

Be aware of “bad smells” in your code, don’t create a singleton unless you are sure that you need it.

--

--

Ali Jaber

iOS Engineer with more than 3 years experience in developing mobile apps in both swift and objective C for both local and international customers