In Swift, you may want to use a singleton in order to ensure only a single instance of your object is ever created. This is very often seen in the facade pattern.
Let's see how to go about creating a singleton:
- Identify the class you want to refactor as a singleton, ensuring it doesn't grow in memory too much over time
- Make the initializer private. A singleton can never be instantiated from outside
- Add a static immutable member, which will be the single instance of your object
- Replace all occurrences of the now inaccessible constructor with the shared reference