Book Image

Swift 2 Design Patterns

By : Julien Lange
Book Image

Swift 2 Design Patterns

By: Julien Lange

Overview of this book

Table of Contents (15 chapters)
Swift 2 Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The singleton pattern


This pattern is certainly the pattern that every developer learns first. It is often used with a factory or abstract factory class to ensure that there is only one instance of the class.

Roles

The singleton pattern ensures that a class has only one instance and provides a global point of access to it, and at this point, it returns an instance of this class.

In some cases, it can be useful to have some classes that have only one instance; for example, in the case of the abstract factory, where it is not useful to have several instances.

Design

The following figure shows the generic UML class diagram of the singleton pattern. There are many way to write the singleton pattern using Swift.

Here, we use the easiest way to do this. With Swift, you'll see that we can change the way in which we apply it, thanks to the class constant:

Participants

There is only one participant in this pattern: the Singleton class.

This class provides a method that returns only one instance of the class. The mechanism locks the creation of other instances. It was introduced with Swift 1.2. We can now use class constants.

With Swift 1.2, we will use the class constants to provide us with a way to ensure the unique creation of the instance.

A class constant is defined as follows:

static let myVariable = myObject()

Collaborations

Every client will have access to the unique instance of the Singleton class by calling the Instance method.

With Swift, the approach we'll consider is the one that accesses our unique instance of the Singleton class using the class constant that we will call sharedInstance.

Illustration

You are developing your card game and you need to manage all the data of the current game. In our game, we have two players; each player has a deck, mana reserve, name, and so on. We have a board (the table where we put our cards) and a game state (who is currently playing). To manage all of this information, you'll need a BoardManager class. This class will be a singleton class because we will not have several boards at the same time (we only allow one game at a time). The singleton pattern can be something interesting that can be used here in order to make sure that we access the good data.

Implementation

The following approach supports lazy initialization, and it is thread safe by the definition of let:

import UIKit

class BoardGameManager {
  
  static let sharedInstance = BoardGameManager()
  init() {
    println("Singleton initialized");
  }
  
}

Usage

To use our singleton object, each client will access it using the following code :

let boardManager = BoardGameManager.sharedInstance

The boardManager variable contains all the members available in our singleton object and will be initialized only once.

This pattern is used in the following cases:

  • We must have only one instance of a class

  • This instance must be accessible to clients from a well-known access point