Creational patterns
The final type of design patterns that we will discuss are called creational patterns. These patterns relate to the initialization of new objects. At first, the initialization of an object probably seems simple and not a very important place to have design patterns. After all, we already have initializers. However, in certain circumstances, creational patterns can be extremely helpful.
A singleton/shared instance
The first patterns we will discuss are the singleton and shared instance patterns. We are discussing them together because they are extremely similar. First, we will discuss shared instance because it is the less strict form of the singleton pattern.
The idea of the shared instance pattern is that you provide an instance of your class to be used by other parts of your code. Let's look at a quick example of this in Swift:
class AddressBook { static let sharedInstance = AddressBook() func logContacts() { // ... } }
Here, we have a simple address...