From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Purpose, pros, and cons

Purpose, pros, and cons - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Purpose, pros, and cons

- [Instructor] The first patten we're goona talk about is the singleton. It's one of the simplest design patterns, but it's probably also the most misused one. The singleton's primary purpose is to guarantee that we can create only one instance of a given type. When would you need this behavior? Singletons can serve you well if there's a single resource, and you need to access and manage that single instance throughout your application. The UIApplication object is a good example. We need one in every IOS app, but we shouldn't have more than one. The singleton UIApplication instance can be accessed using the shared type property. Another example would be a log file handler. You want to ensure that all the logs emitted by the various components of your software system, get inserted into the log file. Also, you need to avoid synchronization issues and data corruption. A singleton can satisfy all these requirements. Having more than one instance of the log file handler is not only…

Contents