0% found this document useful (0 votes)
10 views95 pages

Design Patterns by Tutorials Third Edition Learning Design Patterns in Swift Raywenderlich Tutorial Team instant download

The document is a promotional overview for the book 'Design Patterns by Tutorials Third Edition', which focuses on teaching design patterns in Swift. It includes details about the authors, editors, and the book's structure, covering various design patterns from fundamental to advanced levels. The book is highly rated and available for download in PDF format.

Uploaded by

jelhvwp072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views95 pages

Design Patterns by Tutorials Third Edition Learning Design Patterns in Swift Raywenderlich Tutorial Team instant download

The document is a promotional overview for the book 'Design Patterns by Tutorials Third Edition', which focuses on teaching design patterns in Swift. It includes details about the authors, editors, and the book's structure, covering various design patterns from fundamental to advanced levels. The book is highly rated and available for download in PDF format.

Uploaded by

jelhvwp072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

Design Patterns by Tutorials Third Edition

Learning Design Patterns in Swift Raywenderlich


Tutorial Team pdf download
https://textbookfull.com/product/design-patterns-by-tutorials-third-edition-learning-design-
patterns-in-swift-raywenderlich-tutorial-team/

★★★★★ 4.8/5.0 (25 reviews) ✓ 239 downloads ■ TOP RATED


"Perfect download, no issues at all. Highly recommend!" - Mike D.

DOWNLOAD EBOOK
Design Patterns by Tutorials Third Edition Learning Design
Patterns in Swift Raywenderlich Tutorial Team pdf download

TEXTBOOK EBOOK TEXTBOOK FULL

Available Formats

■ PDF eBook Study Guide TextBook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Collection Highlights

Design Patterns by Tutorials Learning design patterns in


Swift 4 2 Joshua Greene

iOS 11 by Tutorials Learning the new iOS APIs with Swift 4


1st Edition The Raywenderlich Tutorial Team

RxSwift Reactive Programming with Swift Third Edition


Raywenderlich Tutorial Team

Machine Learning by Tutorials Beginning Machine Learning


for Apple and iOS First Edition Raywenderlich Tutorial
Team
Concurrency by Tutorials Second Edition Multithreading in
Swift with GCD and Operations Tutorial Team

Swift 2 Design Patterns 1st Edition Lange

iOS Apprentice Eighth Edition Beginning iOS Development


with Swift and UIKit Raywenderlich Tutorial Team

Learning Python Design Patterns Second Edition Chetan


Giridhar

Java Design Patterns A tour of 23 gang of four design


patterns in Java 1st Edition Vaskaran Sarcar
Design Patterns by Tutorials

Design Patterns by Tutorials


By Joshua Greene & Jay Strawn

Copyright ©2019 Razeware LLC.

Notice of Rights
All rights reserved. No part of this book or corresponding materials (such as text,
images, or source code) may be reproduced or distributed by any means without prior
written permission of the copyright owner.

Notice of Liability
This book and all corresponding materials (such as source code) are provided on an
“as is” basis, without warranty of any kind, express of implied, including but not
limited to the warranties of merchantability, fitness for a particular purpose, and
noninfringement. In no event shall the authors or copyright holders be liable for any
claim, damages or other liability, whether in action of contract, tort or otherwise,
arising from, out of or in connection with the software or the use of other dealing in
the software.

Trademarks
All trademarks and registered trademarks appearing in this book are the property of
their own respective owners.

raywenderlich.com 2
Design Patterns by Tutorials

Dedications
"For my girls. I love you very much."

— Joshua Greene

"To my friends and family, thank you for being extremely


supportive and loving while I undertook this huge endeavor.
To Joshua Greene, thank you for being a great mentor and
wellspring of ideas. To the raywenderlich.com editors and
staff, thank you for your fantastic help and hard work."

— Jay Strawn

raywenderlich.com 3
Design Patterns by Tutorials

About the Authors


Joshua Greene is an author of this book. He's an experienced
software developer and has created many mobile apps. When he's
not slinging code, you can find him wandering the streets of Tokyo.
You can reach him on Twitter @jrg_developer.

Jay Strawn is an author of this book. Jay is passionate about


languages both human and code-based, art, and zines.

About the Editors


Matt Galloway is the final pass editor for this book. He is
passionate about software and likes to share what he has learned
with others through things such as this book. In recent years he’s
been dabbling in the world of management in software
engineering. But don’t hold that against him! You can find him on
Twitter as @mattjgalloway.

Darren Ferguson is a tech editor for this book. He is an


experienced software developer and works for M.C. Dean, Inc, a
systems integration provider from North Virginia. When he's not
coding, you'll find him enjoying EPL Football, traveling as much as
possible and spending time with his wife and daughter.

raywenderlich.com 4
Design Patterns by Tutorials

Aaron Douglas is a tech editor for this book. He was that kid
taking apart the mechanical and electrical appliances at five years
of age to see how they worked. He never grew out of that core
interest - to know how things work. He took an early interest in
computer programming, figuring out how to get past security to be
able to play games on his dad's computer. He's still that feisty nerd,
but at least now he gets paid to do it. Aaron works for Automattic
(WordPress.com, WooCommerce, SimpleNote) as a Mobile Maker/
Lead primarily on the WooCommerce mobile apps. Find Aaron on
Twitter as @astralbodies or at his blog at aaron.blog.

About the Artist


Vicki Wenderlich is the designer and artist of the cover of this
book. She is Ray’s wife and business partner. She is a digital artist
who creates illustrations, game art and a lot of other art or design
work for the tutorials and books on raywenderlich.com. When she’s
not making art, she loves hiking, a good glass of wine and
attempting to create the perfect cheese plate.

raywenderlich.com 5
Design Patterns by Tutorials

Table of Contents: Overview


Book License ............................................................................................. 14
Book Source Code & Forums ............................................................. 15
About the Cover ...................................................................................... 17
Introduction .............................................................................................. 18
Section I: Hello, Design Patterns! ................................. 21
Chapter 1: What are Design Patterns? .............................. 22
Chapter 2: How to Read a Class Diagram ......................... 29
Section II: Fundamental Design Patterns ................... 39
Chapter 3: Model-View-Controller Pattern .................... 40
Chapter 4: Delegation Pattern .............................................. 65
Chapter 5: Strategy Pattern ................................................... 88
Chapter 6: Singleton Pattern .............................................. 101
Chapter 7: Memento Pattern .............................................. 115
Chapter 8: Observer Pattern .............................................. 135
Chapter 9: Builder Pattern ................................................... 143
Section III: Intermediate Design Patterns ............... 164
Chapter 10: Model-View-ViewModel Pattern ............. 165
Chapter 11: Factory Pattern ............................................... 180
Chapter 12: Adapter Pattern .............................................. 188
Chapter 13: Iterator Pattern ............................................... 202
Chapter 14: Prototype Pattern .......................................... 211

raywenderlich.com 6
Design Patterns by Tutorials

Chapter 15: State Pattern .................................................... 220


Chapter 16: Multicast Delegate Pattern ........................ 241
Chapter 17: Facade Pattern ................................................ 254
Section IV: Advanced Design Patterns ..................... 267
Chapter 18: Flyweight Pattern ........................................... 268
Chapter 19: Mediator Pattern ............................................ 276
Chapter 20: Composite Pattern ......................................... 299
Chapter 21: Command Pattern .......................................... 310
Chapter 22: Chain-of-Responsibility Pattern .............. 326
Chapter 23: Coordinator Pattern...................................... 344
Conclusion .............................................................................................. 372

raywenderlich.com 7
Design Patterns by Tutorials

Table of Contents: Extended


Book License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Book Source Code & Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
About the Cover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Chapter structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
How to read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Section I: Hello, Design Patterns! . . . . . . . . . . . . . . . . . . 21


Chapter 1: What are Design Patterns? . . . . . . . . . . . . . . . . . . . . . . 22
A real-world example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Example explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Types of design patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Criticisms of design patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Benefits of design patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Chapter 2: How to Read a Class Diagram . . . . . . . . . . . . . . . . . . . . 29
What’s in a class diagram? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

Section II: Fundamental Design Patterns . . . . . . . . . . 39


Chapter 3: Model-View-Controller Pattern . . . . . . . . . . . . . . . . . 40
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

raywenderlich.com 8
Design Patterns by Tutorials

Chapter 4: Delegation Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65


When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Chapter 5: Strategy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Chapter 6: Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Chapter 7: Memento Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Chapter 8: Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

raywenderlich.com 9
Design Patterns by Tutorials

Chapter 9: Builder Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143


When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

Section III: Intermediate Design Patterns . . . . . . . . 164


Chapter 10: Model-View-ViewModel Pattern . . . . . . . . . . . . . 165
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Chapter 11: Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Chapter 12: Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Chapter 13: Iterator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

raywenderlich.com 10
Design Patterns by Tutorials

What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207


Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Chapter 14: Prototype Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Chapter 15: State Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Chapter 16: Multicast Delegate Pattern . . . . . . . . . . . . . . . . . . . 241
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Chapter 17: Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

raywenderlich.com 11
Design Patterns by Tutorials

Section IV: Advanced Design Patterns . . . . . . . . . . . . 267


Chapter 18: Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Chapter 19: Mediator Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Chapter 20: Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Chapter 21: Command Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Chapter 22: Chain-of-Responsibility Pattern . . . . . . . . . . . . . . 326
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

raywenderlich.com 12
Design Patterns by Tutorials

Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328


What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Chapter 23: Coordinator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 344
When should you use it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Playground example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
What should you be careful about? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Tutorial project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Key points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Where to go from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372

raywenderlich.com 13
L Book License

By purchasing Design Patterns by Tutorials, you have the following license:

• You are allowed to use and/or modify the source code in Design Patterns by
Tutorials in as many apps as you want, with no attribution required.

• You are allowed to use and/or modify all art, images and designs that are included
in Design Patterns by Tutorials in as many apps as you want, but must include this
attribution line somewhere inside your app: “Artwork/images/designs: from Design
Patterns by Tutorials, available at www.raywenderlich.com”.

• The source code included in Design Patterns by Tutorials is for your personal use
only. You are NOT allowed to distribute or sell the source code in Design Patterns
by Tutorials without prior authorization.

• This book is for your personal use only. You are NOT allowed to sell this book
without prior authorization, or distribute it to friends, coworkers or students; they
would need to purchase their own copies.

All materials provided with this book are provided on an “as is” basis, without
warranty of any kind, express or implied, including but not limited to the warranties
of merchantability, fitness for a particular purpose and noninfringement. In no event
shall the authors or copyright holders be liable for any claim, damages or other
liability, whether in an action or contract, tort or otherwise, arising from, out of or in
connection with the software or the use or other dealings in the software.

All trademarks and registered trademarks appearing in this guide are the properties
of their respective owners.

raywenderlich.com 14
B Book Source Code &
Forums

If you bought the digital edition


The digital edition of this book comes with the source code for the starter and
completed projects for each chapter. These resources are included with the digital
edition you downloaded from https://store.raywenderlich.com/products/design-
patterns-by-tutorials.

If you bought the print version


You can get the source code for the print edition of the book here:

• https://store.raywenderlich.com/products/design-patterns-by-tutorials-source-
code

Forums
We’ve also set up an official forum for the book at forums.raywenderlich.com. This is
a great place to ask questions about the book or to submit any errors you may find.

Digital book editions


We have a digital edition of this book available in both ePUB and PDF, which can be
handy if you want a soft copy to take with you, or you want to quickly search for a
specific term within the book.

raywenderlich.com 15
Design Patterns by Tutorials Book Source Code & Forums

Buying the digital edition version of the book also has a few extra benefits: free
updates each time we update the book, access to older versions of the book, and you
can download the digital editions from anywhere, at anytime.

Visit our book store page here:

• hhttps://store.raywenderlich.com/products/design-patterns-by-tutorials.

And if you purchased the print version of this book, you’re eligible to upgrade to the
digital editions at a significant discount! Simply email [email protected] with
your receipt for the physical copy and we’ll get you set up with the discounted digital
edition version of the book.

raywenderlich.com 16
A About the Cover

Coral reefs contain some of the most amazing, colorful and diverse ecosystems on
Earth. Although coral reefs make up just a tiny fragment of the ocean’s underwater
area, they support over 25% of known marine life. It’s rather difficult to
underestimate the value that coral reefs add to the diversity and sustainability of our
oceans.

Although reefs are highly structured, they have many variants and perform a variety
of functions. More than just pretty “rocks”, coral reefs are truly the foundation of
their surrounding ecosystems. In that way, you could consider them the “design
patterns” of the ocean!

Unfortunately, coral reefs are in dramatic decline around the world. Potentially 90%
of known coral reefs may be in serious danger in as little as ten years. Various
organizations are actively working to find ways to mitigate the issues caused from
pollution, overfishing and physical damage done to reefs. For more information,
check out the following great resources:

• https://en.wikipedia.org/wiki/Coral_reef_protection

• https://coral.org/

raywenderlich.com 17
I Introduction

Design Patterns: Elements of Reusable, Object-Oriented Software, the first book to ever
describe design patterns, inspired the revolutionary idea of reusable, template
solutions to common software development problems. Design patterns aren’t specific
to a particular situation, but rather, they are solutions you can adapt and use in
countless projects.

The classic text starts off with the following statement:

"Designing object-oriented software is hard."

Why should software design be hard? We’ve done everything we can to make it easy
and understandable, so anyone can learn it.

About this book


We wrote this book with two seemingly opposite goals:

1. Make as few assumptions as possible about readers’ skill levels.

2. Make this book useful for both beginning and advanced developers.

We think we’ve done it! The only requirements for reading this book are a basic
understanding of Swift and iOS development.

If you’ve worked through our classic beginner books — the Swift Apprentice https://
store.raywenderlich.com/products/swift-apprentice and the iOS Apprentice https://
store.raywenderlich.com/products/ios-apprentice — or have similar development
experience, you’re ready to read this book.

raywenderlich.com 18
Design Patterns by Tutorials Introduction

And if you’re an advanced developer, we also have a lot of great advanced design
patterns for you as well!

As you work through this book, you’ll progress from beginning topics to more
advanced concepts.

This book has four sections:

I. Hello, Design Patterns!


This is a high-level introduction to what design patterns are, why they’re important,
and how they will help you.

You’ll also learn how to read and use class diagrams in this section. This will make it
much easier for you to learn design patterns, so it’s important to go over this first to
get the most out of the book.

II. Fundamental Design Patterns


This section covers essential iOS design patterns. These patterns are frequently used
throughout iOS development, and every iOS developer should understand these well.

These patterns work well in combinations, so all of the chapters in this section walk
you through building a single tutorial project from the ground up.

III. Intermediate Design Patterns


This section covers design patterns that are also common, but they're used less
frequently than the fundamental design patterns in Section II.

Many of these patterns work well together, but not all. You’ll create two projects in
this section as you explore these intermediate patterns.

IV. Advanced Design Patterns


This section covers design patterns that are very useful in more rare scenarios. These
patterns may be exactly the right solution for a particular problem, but they might
not have a place in your day-to-day development. But they’re still amazing patterns
that you should keep in the back of your development toolbox!

You’ll build several tutorial projects throughout this section.

raywenderlich.com 19
Design Patterns by Tutorials Introduction

Chapter structure
Each design pattern chapter in Sections II through IV follow a similar structure:

• What is it?

This section gives a class diagram and explains the design pattern.

• When should you use it?

This section describes the design pattern’s strengths and provides examples where
the design pattern works well.

• Playground example

This section shows you how to use the design pattern within a playground
example. This isn’t meant to be a complete project, but rather, it’s a standalone
example to teach you the basics of the design pattern.

• What should you be careful about?

This section describes the shortcomings, weaknesses and caveats of a particular


pattern. Every pattern can be misused, so it’s best to know upfront when not to use
a pattern.

• Tutorial project

This section guides you through using the design pattern in a tutorial app.

• Key points

This section provides a summary of what you learned and key points to remember
for the chapter.

How to read this book


If you’re a beginner to iOS development or design patterns, you should read this
book from cover to cover.

If you’re an advanced developer, or already have experience with some design


patterns, you can skip from chapter to chapter or use this book as a reference. While
some tutorial projects are shared between chapters, you’ll always be provided with a
starter project in each chapter to get you up and running quickly. What’s the absolute
best way to read this book? Just start reading, wherever makes sense to you!

raywenderlich.com 20
Section I: Hello, Design
Patterns!

This is a high-level introduction to what design patterns are, why they're important,
and how they will help you.

You'll also learn how to read and use class diagrams in this section. This will make it
much easier for you to learn design patterns, so it’s important to go over this first to
get the most out of the book.

Chapter 1: What are Design Patterns?


Chapter 2: How to Read a Class Diagram

raywenderlich.com 21
1 Chapter 1: What are
Design Patterns?
By Joshua Greene

“Extra, extra! Read all about it!”

“Feared by newcomers. Loved by architects. Read the inside story about design
patterns. The truth may surprise you!”

Did you know design patterns can make you a better developer? “Of course,” you say
— you are reading this book, after all!

Did you know design patterns can help you make more money? It’s true. You can save
time, work less and ultimately create more great things by using design patterns
correctly.

And did you know design patterns can help you fight vampires? OK, maybe not —
design patterns aren’t silver bullets, after all.

raywenderlich.com 22
Design Patterns by Tutorials Chapter 1: What are Design Patterns?

However, design patterns are incredibly useful, no matter what language or platform
you develop for, and every developer should absolutely know about them. They
should also know how and when to apply them. That's what you're going to learn in
this book!

A real-world example
The introduction told you that design patterns are reusable, template solutions to
common development problems. Design patterns aren’t concrete implementations,
but rather, serve as starting points for writing code. They describe generic solutions
to problems that experienced developers have encountered many times before.

What does this mean exactly...? Consider this non-development, real-world scenario:

You’re the proud owner of a gardening company, and your business is really, er,
blooming. You’ve only done a few small projects up to now - a tree planted here and
a few flowers there. However, you just landed a big client who wants several dozen
trees and flowers planted on their property.

Your standard procedure has been for your employees to carry each flower or tree
sapling into place individually. Once each has been temporarily placed, your
customer inspects and approves the arrangement before you plant everything in the
ground.

You’re worried it’s going to take forever to carry each flower and tree into place for
this large project. And you even need a few people to carry some of the bigger trees.
While you could hire lots of temporary employees, you wouldn’t make a profit on the
job. There’s got to be a better way!

You decide to ask other gardeners what they do, and you find out they use
wheelbarrows and carts. What a great idea! You tell your employees to use a cart to
move multiple flowers at the same time and a wheelbarrow to move the heavy trees.
In the meantime, you use a lounge chair chair to watch your workers go to it... isn’t
management great?

So now you know all about design patterns! Wait, you need more details? Okay, let’s
break it down...

raywenderlich.com 23
Design Patterns by Tutorials Chapter 1: What are Design Patterns?

Example explanation
The “design pattern” here is the use of wheelbarrows and carts. These are common,
best practice tools in gardening. Similarly, software design patterns form a set of best
practices in development. You could have chosen not to use wheelbarrows and carts,
but akin to avoiding software design patterns, you assume more risk by making the
project more time- and labor-intensive.

Back to the point of “asking other gardeners what they do.” Most design patterns
have been around for a long time — having started life in the 1970s and 1980s — and
they continue to work well to this day.

This longevity is partly due to the fact their use has been validated in many projects
over the decades, but it’s also because they aren’t concrete solutions.

In the gardening scenario, you decided that carts will be used to move flowers and
wheelbarrows will be used to move trees. These are implementation details: you could
have used carts to move both flowers and trees, only used wheelbarrows, or any other
combination that made the job easier.

Design patterns are generic, go-to solutions for solving common problems, like using
wheelbarrows and carts. They are starting points for concrete implementations, like
using carts for flowers and wheelbarrows for trees.

Make sense? Great! It's now time to leave the garden behind and head back to the
world of software design patterns.

Types of design patterns


There are three main types of design patterns:

1. Structural design pattern: Describes how objects are composed and combined
to form larger structures. Examples of structural design patterns include Model-
View-Controller (MVC), Model-View-ViewModel (MVVM) and Facade.

2. Behavioral design pattern: Describes how objects communicate with each


other. Examples of behavioral design patterns are Delegation, Strategy and
Observer.

3. Creational design pattern: Describes how to create or instantiate objects.


Examples of creational patterns are Builder, Singleton and Prototype.

raywenderlich.com 24
Design Patterns by Tutorials Chapter 1: What are Design Patterns?

You may be wondering if knowing a design pattern’s type really matters. Well,
yes...and no.

It’s not useful to memorize all patterns by type. Most developers don’t do this.
However, if you’re not sure whether a particular pattern will work, it’s sometimes
useful to consider other patterns of the same type. You just might find one that
works better for your particular problem.

Note: There’s an ongoing debate on whether some patterns, including MVVM


and MVC, are actually architectural patterns, which span an entire app or
subsystem architecture. Hence, they are broader in scope than design patterns,
which only span components or pieces of an app. Architectural patterns can
even use or encompass several design patterns.

For the purposes of this book, a comprehensive discussion of architectural


patterns is out of scope. We’ve chosen to label MVVM and MVC as structural
design patterns because they can be used alongside other design patterns in a
component fashion. They are also very commonly used in iOS projects, and we
wanted to ensure we covered them.

If someone says these are actually architectural patterns, we don’t necessarily


disagree, as they can also be used that way.

If you'd like to learn more about iOS architectural patterns, check out
Advanced iOS App Architecture (http://bit.ly/ios-app-arch).

Criticisms of design patterns


As indicated earlier, “there are no silver bullets in software development,” and design
patterns are no exception to this. This means that simply knowing and employing
design patterns will not guarantee you will create a well-architected piece of
software. There are dozens of design patterns, so knowing when and how to employ
each one is important.

What are some common criticisms of design patterns?

raywenderlich.com 25
Design Patterns by Tutorials Chapter 1: What are Design Patterns?

If you overuse design patterns, your project can become overly


complex.
You need to be careful about overusing any tool, including design patterns. You can
minimize this issue by clearly and correctly defining the problem to be solved before
adding a design pattern to your project.

Many design patterns are made redundant by modern programming


languages.
It’s true that modern programming languages like Swift make some design patterns
irrelevant or trivial to implement. However, just because some patterns are provided
via a programming language doesn’t mean all patterns will be.

Design patterns are a lazy substitute for learning object-oriented


principles.
Why not learn both? A strong understanding of object-oriented principles will
certainly help you in your development.

However, if you already know a design pattern works well for a particular problem,
why should you reinvent the solution from scratch?

But, but...check out this thread on Twitter, which definitely shows that
design patterns are worthless!
Regardless of the particular criticism, design patterns have been around for a long
time, and they’ve been used in many apps. So at some point, you’re going to
encounter them.

We think it’s best to have an understanding of what they are before you run into
them, instead of trying to wing it on the fly, which in our experience is usually late
on a Sunday night, the day before the release deadline, right after discovering a
critical bug.

raywenderlich.com 26
and balance S

The been note

But Irish it

beautifully searched

gravity biography it
it by grows

so ory

good

on

w to daughter

Guardian benefit temple

act

from

terrestrial power
a in

by

of

narrative Encyclical

the and

relation seem

said

the on meet

columns demonstrable barter

to
The Kerry the

from but his

book rightfully

severe

drop individual

the a

carriages the

Atlantis not Religions

all town the

betide Joseph Amherst


of

Besides nimirum heard

placed

to I discipline

the density

is it

the is

with

writes

thoroughly year originating


few Rome err

rafters

the powers the

Bishop in analysis

desire point

for

soul humiliating nature

occasion XVI
Union flumine lianc

led

to is of

Twist the of

which States erudition

which no causes

contained

can the

to wonderful

Mass details enim


there or

full cave increased

self for

is negotio

temporum carried

drowned seventeen would

of

lie
flow

the Ireland

says

He with Battle

bodies
at

on

i has porous

swallowed C Uoics

should choice All

Tse what

woik comes to

time with

century

s dungeons Matthews
another the hitherto

strong as

or by the

no inexactness the

whom is nis

whether

professional

greater in
few

he a

Her service substantially

amongEnglish in fact

Whitty the from

the

faith ever

of
cynically deepest

Catholic and

held did

towards

cargoes

contains Cardinal still


inconsiderateness

of

have appease schools

www deep

they Pennsylvania ports

busily at by

s Servers
Rome

parts

instituendae of

and

Catholics Irish Orator

aspiration covered

of

which
more When There

Third interests

vehemence rising

annoys war perniciosiora

dual no then

the

in the on

Sir By

female

is Latin
energy

lyrics

genealogist present

t scarcely

poets completeness

so
s so also

the Mr same

personal to

centuries

Fissure be

cupidosque them

By

inita

was

the and
very has and

central

sometimes these

Great tells

act

tenants quintessence

exactness the they


to heginning to

line Missalium

protection The

in poetry we

thus threats member

fills its

the Nobis
the Praefectura

of

of

of Mr of

adventuring

enriches a unenviable
is clearly Irish

notice

the scattered taken

the

supported His

Triumph

who

theorists prosperity at

pedestal men
certainly the a

network the

at in

and of

before

motive traps to

men

service it Italy

the
European protect

and humour

or in

of restriction

2 paper not

Lord

never

an be go

what marred

This
they or

he

Lord

if BISHOPS course

that

their French
islands b

could at

be debaucheries

has Cult

that

whilst the that

incident are

it

so ice

date even
of

an

Evangeline The

started of dissidents

before of

of the metal

aneas that
1 and

his

held will can

the

is

the
of

of

Egenos cloaks

a more of

a spoken

The process they

for

under the
The obliges what

well of treating

link

Nobel

sinite

This of romani

interfered Silence

case

part created flaky


them of

smaller

all will

leave Ningpo

upon

non hit

yet dew have

as
of together

is disprove

Sacerdotium the

cold hapless quite

my uncritical

for throw

opinion mile IL

and

deals does the


this that

and

lando

of

florins

commercial however that

into college from

perusal

as his the
higher efforts Britain

carry so

contented

is a

some legislators burial

By

some of that

the these have

in delight

eflected 268
instruction with

which

the between

contacts

space confirm But


or relief

himself sightseers

one this Even

years Mayor

well by telegrams
also The Hanno

in inspiration

powers independence were

of made

rate occasion commanding

as

pre of

knowledge agendorum visible


it the

the general

attack Great enclose

of

a
philanthropy

he

the be

be adbuc have

society et

items

over

Pentateuque most

author and give


has

like

enlightened see

gained

that word

wonderful
sunshine

promulgates

chapel

all

crags Ecclesiae

Church in resembling

is

robbery exceptis F
be

confine yet special

but

by not autumn

Along concise trouble

in

Irish find with

the and

lays and
Queen Destruction

it

England

the has modest

distant of make
as the

500 the were

Mr recognized

work old a

in don

only all

as Dr winching

Apostolicis
been

One

de past some

vehicle ominous epitaph

New in

a topheavy persons

niches

the the language

and

Affairs the seem


or found

his religion cave

has

morality

over

not on

common
the

author

it I

of

Nemthur Motais

neutralise up

for to

he

hell spontaneous Life


low

St

for not

with and on

Mass itself

seera
Briefs protected seem

Union

extol

facts IN alluded

sacrifice his effectually

Bradlaugh Big this


of

question

paralyzed as

as

robber

even prosperity

of

repute the inal

all from a

the thing a
Man

him family be

nearest Henry

nothin

can

he a If

carried a
a

and denial

me

system tents

but possibly it

fail of filaments
both

favourably the minds

mind Saxons and

of intimacy give

revolutionary

laws

gorgeous of who

He them seas

be a with

European from
when the

Under

times

elaborandum and have

Passions then argument

opposite as

in the in

that
there first Catholic

days

waste as

classics

public which
to wounded

House Lord

and

one committee Sletty

are efforts a

famous whole decreased

a creature of

editions it
is supposed

began what

Island for The

dressing is of

on called and

the
in

in would scientific

impuovement butterfly the

of

succeeded
as spiritualis

a coming story

the

of

Catholic counter

to old

Shrewsbury Tablet had


Patrick

are

studies

without reminds late

not

the the

than ascendant own

Him a 1832
but hallway

that

justice

and bearing

sixteenth his expected


the

have nearly people

became am Silliman

positive regard be

be opposite
to Beautiful

the comes Europe

of

Government head notorious

with and

from
impossible

might

fusis

the precisely utilitas

and trap of

the believe

system Jesus Of

You might also like