- Swift 2 Design Patterns
- Julien Lange
- 622字
- 2021-07-09 21:25:41
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
Participants
Participant to this pattern are as follows:
Client
: This class contains a list of objects called prototypes that are instances of theAbstractPrototype
abstract class. TheClient
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 calledclone()
.ConcretePrototype1
andConcretePrototype2
: These are concrete classes that inherit from theAbstractPrototype
class. They define a prototype and have both a cloning method calledclone()
.
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:

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) } }
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:

- Getting Started with React
- 企業(yè)級(jí)Java EE架構(gòu)設(shè)計(jì)精深實(shí)踐
- Oracle從新手到高手
- Xcode 7 Essentials(Second Edition)
- Learn WebAssembly
- Python機(jī)器學(xué)習(xí)基礎(chǔ)教程
- Learning Hunk
- Scala程序員面試算法寶典
- Learning Apache Karaf
- .NET 4.5 Parallel Extensions Cookbook
- SQL Server 2008中文版項(xiàng)目教程(第3版)
- Clojure High Performance Programming(Second Edition)
- Vue.js 3.x高效前端開(kāi)發(fā)(視頻教學(xué)版)
- Tkinter GUI Programming by Example
- Unreal Engine Game Development Cookbook