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 objectives of these patterns are described in the following table:
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.
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:
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()
.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:
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) } }
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: