Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift 2 Design Patterns
  • Table Of Contents Toc
Swift 2 Design Patterns

Swift 2 Design Patterns

By : Lange
3 (2)
close
close
Swift 2 Design Patterns

Swift 2 Design Patterns

3 (2)
By: Lange

Overview of this book

Swift is a multi-paradigm language. It has expressive features familiar to those used to work with modern functional languages, while also keeping the object-oriented features of Objective-C. It remains compatible with Apple’s legacy codes and frameworks. A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples. Knowledge about design patterns is also one of the best ways to make you different compared to other low-level developers. This book shows you how to use Swift 2 to learn about 23 Gang of Four (GoF) design patterns, and is organized into three categories. The book will present you the five creational patterns, followed by the seven structural patterns, and finishing with the 11 behavioral patterns as defined by the GoF. Each chapter will introduce the pattern by defining its role, which common problems the pattern should be used for, its generic UML representation, how each objects presented in the class diagram participate in the pattern, and what the role of each of these objects is. The book then presents you with a concrete case as an illustration that will be used to implement the pattern using Swift.
Table of Contents (10 chapters)
close
close
9
Index

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
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Swift 2 Design Patterns
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon