Skip to content

The Singleton Design Pattern

Published on:January 6, 2023 at 12:00 PM

What is a Singleton?

The singleton design pattern is a design pattern that ensures that a class has only one instance and provides a single point of access to it. This is useful when exactly one object is needed to coordinate actions across the system. It can further ensure that no other instance can be created.

In Swift, the singleton design pattern can be implemented as follows:

class Singleton {
    static let shared = Singleton()
    private init() {}
}

This class creates a constant called shared which is an instance of Singleton, it can be accessed from anywhere by using Singleton.shared.

Notice that the initializer is private to prevent other parts of the app from creating additional instances of the class. Here’s an example of how the singleton can be used:

class NetworkManager {
    static let shared = NetworkManager()
    private init() {}

    func get(_ url: URL, completion: @escaping (Result<[Post], Error>)) {
        // Make network request
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NetworkManager.shared.get(URL(string: "https://alfredohdz.io/posts")!) { _ in
            // Handle result
        }
    }
}

In this example, the NetworkManager is a singleton class that handles network requests for the app. The get(_ url: URL) function can be called from anywhere in the app by accessing the shared instance of NetworkManager.

There are several pros and cons to using the singleton design pattern:

Pros:

Cons:


In general, it’s important to weigh the pros and cons of using the singleton design pattern and choose the pattern that is most appropriate for the specific needs of the app.