Chapter 1. Creational Patterns

The creational patterns are designed to deal with the object creation mechanism in software designing. A system using these patterns becomes independent of how objects are created, which means it is independent of how concrete classes are instantiated.

These patterns encapsulate the use of concrete classes and favor the use of interfaces in the relationship between objects, allowing to have better abstraction of the global system conception.

Thus, if we analyze the singleton pattern, a pattern designed to instantiate only one instance of a class, we find that the mechanism that controls the unique access to this instance is fully encapsulated in the class, which means that this is completely transparent to the client consuming the instance of the class.

In this chapter, we will introduce you to the five creational patterns and discuss how we can use them with Swift:

  • The prototype pattern
  • The factory method pattern
  • The singleton pattern
  • The abstract factory pattern
  • The builder pattern

The objectives of these patterns are described in the following table:

Pattern

Objective

The prototype pattern

This pattern allows you to create new objects by duplicating existing objects called prototypes. This pattern has the cloning capability.

The factory method pattern

This pattern introduces you to an abstract method that allows you to create an object by telling its subclasses about the effective creation of the object.

The singleton pattern

This pattern ensures that a class has only one instance. This class provides a unique point of access that returns this instance.

The abstract factory pattern

This pattern allows you to create an object that is grouped in families by hiding the concrete classes that are needed to create these objects.

The builder pattern

This pattern allows you to separate the creation of complex objects from their implementation. This permits a client to create complex objects having different representations.

The prototype pattern

Our first pattern will be the prototype pattern; we will see how we can use it to accelerate the creation of an instance. We will see how we can use it to copy an existing instance, and eventually, we will see how to modify the new one to our needs.

Roles

The prototype pattern is used to create a new object by duplicating existing objects called prototypes, and they have a cloning capability.

This pattern is used in the following use cases:

  • When you need to create an instance without knowing the hierarchy of a class
  • When you need to create class instances that are dynamically loaded
  • When you need to have a simple object system and not include a parallel hierarchy of a factory class

Design

The following diagram shows the generic class of the prototype pattern:

Design

Participants

Participant to this pattern are as follows:

  • Client: This class contains a list of objects called prototypes that are instances of the AbstractPrototype abstract class. The Client class needs to clone these prototypes without having to know their internal structure and subclass hierarchy.
  • AbstractPrototype: This is an abstract class that can duplicate itself. This class contains a cloning method called clone().
  • ConcretePrototype1 and ConcretePrototype2: These are concrete classes that inherit from the AbstractPrototype class. They define a prototype and have both a cloning method called clone().

Collaborations

The client asks to one or more prototypes to clone themselves.

Illustration

A simple and real example of where this pattern can be applied is the famous game HeartStone from Blizzard (the creator of World of Warcraft). In this strategy card game, when you spend "mana" to use spells, weapons, or put a minion on the board, there is a special minion that has the ability to clone a particular card. When a player uses this card, it selects the minion that he or she wants to clone and the card becomes an exact copy of the selected card. The following card represent the "HeartStone" card that have this behavior:

Illustration

Implementation

The following code represent the implementation of the pattern using Swift:

import UIKit

class AbstractCard {
  var name: String?
  var mana: Int?
  var attack: Int?
  var defense: Int?
  
  init(name:String?, mana:Int?, attack:Int?, defense:Int?) {
    self.name = name
    self.attack = attack
    self.defense = defense
    self.mana = mana
  }
  
  func clone() -> AbstractCard {
    return AbstractCard(name: self.name, mana: self.mana, attack: self.attack, defense: self.defense)
  }
}

class Card: AbstractCard {
  
  override init(name:String?, mana:Int?, attack:Int?, defense:Int? ) {
    super.init(name: name,mana: mana,attack: attack,defense: defense)

  }
}

Note

The AbstractPrototype class is our AbstractCard class, where we implement a way to return a copy of itself using the clone() method.

Usage

The following code simulate how the client will interact with the Card object which implement the prototype pattern:

// Simulate our client

// This is the card that we will copy
let raidLeader = Card(name: "Raid Leader", mana: 3, attack: 2, defense: 2)

// Now we use our faceless Manipulator card to clone the raidleader
let facelessManipulator = raidLeader.clone()

print("(facelessManipulator.name, facelessManipulator.mana, facelessManipulator.attack, facelessManipulator.defense)")

Since the code is written in a Playground file, you should consider it as the code that you'll put in the Client class.

First, we instantiate a new card named Raid Leader. This is a concrete prototype class. Let say that you have the "Faceless Manipulator" card and you want to use it to clone the "Raid Leader" card, then you simply need to use the raidLeader.clone() method that will return a new instance with the exact same properties as "Raid Leader".

By checking the details on the right-hand side of the Playground file, you'll see that the facelessManipulator constant has exactly the same properties as raidLeader (line 39), as shown in the following screenshot:

Usage
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset