Design Patterns by Tutorials Learning design patterns in Swift 4 2 Joshua Greene pdf download
Design Patterns by Tutorials Learning design patterns in Swift 4 2 Joshua Greene pdf download
https://textbookfull.com/product/design-patterns-by-tutorials-
learning-design-patterns-in-swift-4-2-joshua-greene/
https://textbookfull.com/product/design-patterns-by-tutorials-
third-edition-learning-design-patterns-in-swift-raywenderlich-
tutorial-team/
https://textbookfull.com/product/swift-2-design-patterns-1st-
edition-lange/
https://textbookfull.com/product/learning-python-design-patterns-
second-edition-chetan-giridhar/
https://textbookfull.com/product/java-design-patterns-a-tour-
of-23-gang-of-four-design-patterns-in-java-1st-edition-vaskaran-
sarcar/
Java design patterns a tour of 23 gang of four design
patterns in Java 1st Edition Vaskaran Sarcar
https://textbookfull.com/product/java-design-patterns-a-tour-
of-23-gang-of-four-design-patterns-in-java-1st-edition-vaskaran-
sarcar-2/
https://textbookfull.com/product/ios-test-driven-development-by-
tutorials-first-edition-learn-real-world-test-driven-development-
joshua-greene/
https://textbookfull.com/product/api-design-patterns-meap-v07-jj-
geewax/
https://textbookfull.com/product/ios-11-by-tutorials-learning-
the-new-ios-apis-with-swift-4-1st-edition-the-raywenderlich-
tutorial-team/
https://textbookfull.com/product/mastering-php-design-patterns-
develop-robust-and-reusable-code-using-a-multitude-of-design-
patterns-for-php-7-ali/
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
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
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
Dedications
"For my girls — Marsha, Madeline and Amelia. Thank you for your
patience and support. You mean the world to me, and 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
WOW! eBook
www.wowebook.org
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.
raywenderlich.com 4
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 5
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 6
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 7
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 8
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 9
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 10
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 11
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 12
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials
raywenderlich.com 13
WOW! eBook
www.wowebook.org
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.
Why should software design be hard? We’ve done everything we can to make it easy and
understandable, so anyone can learn it.
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 14
WOW! eBook
www.wowebook.org
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.
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.
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.
Many of these patterns work well together, but not all. You’ll create two projects in this
section as you explore these intermediate patterns.
raywenderlich.com 15
WOW! eBook
www.wowebook.org
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.
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.
• 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.
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 16
WOW! eBook
www.wowebook.org
L Book 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 17
WOW! eBook
www.wowebook.org
B Book Source Code &
Forums
This book comes with the source code for the starter and completed projects for each
chapter. These resources are shipped with the digital edition you downloaded from
store.raywenderlich.com.
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.
raywenderlich.com 18
WOW! eBook
www.wowebook.org
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 19
WOW! eBook
www.wowebook.org
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.
raywenderlich.com 20
WOW! eBook
www.wowebook.org
1 Chapter 1: What are Design
Patterns?
By Joshua Greene
“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.
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!
raywenderlich.com 21
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 1: What are Design Patterns?
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
maybe 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 the flower has been temporarily placed in the
flowerbed, 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 into place 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...
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.
raywenderlich.com 22
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 1: What are Design Patterns?
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.
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.
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.
raywenderlich.com 23
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 1: What are Design Patterns?
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.
raywenderlich.com 24
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 1: What are Design Patterns?
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 25
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 1: What are Design Patterns?
Key points
In this chapter, you learned what design patterns are and why you should care about
them. Here are the key points to remember:
• Design patterns aren't concrete implementations, but rather, they are a starting
point for writing code.
• Design patterns collectively form a set of best practices to help you write more
understandable and easier-to-maintain code.
• There are three main types of design patterns: structural, behavioral and creational.
• There are both criticisms and benefits of design patterns. Ultimately, they are
commonplace in software development, and you're likely to encounter them.
Therefore, having a good grasp of them is important.
raywenderlich.com 26
WOW! eBook
www.wowebook.org
2 Chapter 2: How to Read a
Class Diagram
By Joshua Greene
So now you know what design patterns are! In this chapter, you’re going to learn about
a fundamental concept to help you understand design patterns: the class diagram.
Class diagrams are like engineering blueprints; they provide information about a
system through the medium of pictures, symbols and annotations.
You may have heard of Unified Modeling Language (UML), which is a standard language
for creating class diagrams, architectural drawings and other system illustrations. A
complete discussion of UML is beyond the scope of this book, but you won’t need to
understand a lot of UML in your day-to-day iOS development. Instead, you’ll learn a
subset of UML in this chapter that’s useful for creating class diagrams and describing
design patterns.
A box denotes a class. Here’s a very simple class diagram for a Dog class:
raywenderlich.com 27
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 2: How to Read a Class Diagram
To indicate that one class inherits from another, use an open arrowhead:
But instead of reading this as “inherits from,” read this as “is a”. For example, to show
that SheepDog inherits from Dog, you’d draw the following diagram:
Class diagrams can be written from bottom to top, from left to right, or in any other
orientation you’d like. Regardless of the orientation you choose, the direction of the
arrows define the meaning: Inheritance arrows always point at the superclass, and
property arrows always point at the property class.
raywenderlich.com 28
WOW! eBook
www.wowebook.org
Design Patterns by Tutorials Chapter 2: How to Read a Class Diagram
You should read a property arrow as “has a.” For example, if a Farmer has a Dog, you’d
draw this:
You should always use the singular form of the class name in class diagrams, even if
you’re conveying a one-to-many relationship. In this case, you should write Dog, not
Dogs.
raywenderlich.com 29
WOW! eBook
www.wowebook.org
Discovering Diverse Content Through
Random Scribd Documents
IX
Dos o tres expulsados, tres meses sin salida los domingos a casi
todos e interminables horas de encierro a muchos de nosotros
volvieron a poner las cosas en su estado normal, afirmándose
definitivamente la disciplina con el ingreso de Don José M. Torres.
El encierro es un recuerdo punzante que no me abandona; eterno
candidato para ocuparlo, su huésped frecuente, conocía una por una
sus condiciones, sus escasos recursos, sus numerosas inscripciones
y aquel olor húmedo, acre, que se me incrustaba en la nariz y me
acompañaba una semana entera. La puerta daba a un descanso de
la escalera que se abría frente al gimnasio.—Era una pieza baja, de
bóveda: cuatro metros cuadrados. Tenía un escaño de cal y canto,
demasiado estrecho para acostarse y que daba calambres en la
espalda a la hora de estar sentado en él. Una luz insignificante
entraba por una claraboya lateral y muy alta, por donde los
compañeros solían tirar con maestría algunos comestibles con que
combatir el clásico régimen de pan y agua.
¡Oh! las horas mortales pasadas allí dentro, tendido en el suelo, llena
de tierra la cabeza, el cuerpo dolorido, los oídos tapados para no oir
el ruido embriagador de la partida de rescate, en la que yo era
famoso por mi ligereza, la vela de sebo, mortecina y nauseabunda,
pegada a la pared, debajo de una caricatura de Paunero con
tricornio y con una cinta saliendo de su boca, a manera de las
ingenuas leyendas brotando de labios de vírgenes y santos, en el
arte cristiano primitivo, pero cargada aquí con un dístico cojo y
expresivo; la enorme hoja de la puerta, tallada, quemada de arriba
abajo, horadada y recompuesta, como un pantalón de marinero; la
cerradura claveteada y cosida, fiel e incorruptible, virgen de todo
atentado, desde la solemne declaración de Corrales sobre la
ineficacia de nuevas tentativas al respecto; el hambre frecuente, los
proyectos de venganza negra y sombría, lentamente madurados en
la obscuridad, pero disipados tan pronto como el aire de la libertad
entraba en los pulmones!...
He conservado toda mi vida un terror instintivo a la prisión; jamás
he visitado una penitenciaría sin un secreto deseo de encontrarme
en la calle. Aun hoy las evasiones célebres me llenan de encanto y
tengo una simpatía profunda por Latude, el barón de Trenck y
Jacques Casanova. No he podido comprender nunca el libro de Silvio
Pellico, ni creo que el sentimiento de conformidad religiosa, unido a
un imperio absoluto de la razón, basten para determinar esa placidez
celeste, si no se tiene una sangre tranquila y fría, un espíritu
contemplativo y una atrofia completa del sistema nervioso.
XXI
Levántasi, muchachi,
que la cuatro sun
e lo federali
sun vení o Cordun.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com