Design Patterns in Swift
Ch 0 Introduction
Sai Li @ Yowoo Tech. 2017/3/17
Agenda
• Prologue • What is Pattern? • OOP Recap
2
• After OOP • Clean Code, OOP, Design Pattern,
Refactor, Platform Framework, Test • Future interview
3
know nothingwork
What is a Pattern?
Christopher Alexander (1936 - )
Each pattern is a three-part rule, which expresses a relation between a certain context, a problem, and a solution.
Christopher Alexander (1979, p. 247)
Pattern is a solution to a problem in a context
• Context: A CEO of a company
• Problem: tough competition among the delivery services, all restaurants were closed by other company. How to make users order other food?
• Solution: 8 + 9 = 17
Pattern is a solution to a recurring problem in a context
• Context: A CEO of a company
• Problem: tough competition among the delivery services. How to get more orders?
• Solution: Binding card then ordering for free. Threaten to selling card information and shut down server.
Pattern is a proven solution to a recurring problem in a context
• Context: A CEO of a company
• Problem: tough competition among the delivery services. How to get more orders?
• Solution: Send coupons, hold activities. Buy UberEATs, FoodPanda…
Pattern is a proven solution to a recurring problem in a
specific context
• Context: tough competition among the delivery services. A CEO of a company with limited budget
• Problem: How to get more orders?
• Solution: Send coupons, hold activities …
Pattern is a proven solution to a recurring problem in a specific context
Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.
— Christopher Alexander
16
17
• Name • Context • Problem • Solution • Force • Resulting Context
ContextProblem
Elements of Pattern
Solution(Form)Force
Why learn Design Patterns?
• Common ground for communication • Not for excluding outsiders • • Reusable is so hard • Standing on the shoulders of giants - Issac Newton
18
20
Erich Gamma, Richard Helm,
Ralph Johnson, John Vlissides
Gang of Four (GoF)1994 ~
References
by Teddy Chen
21
• Purpose 1. Creational 2. Structural 3. Behavioral
22
• Scope 1. Class: static 2. Object: dynamic
purpose
creational structural behavioral
scope
class Factory Method Adaptor Interpreter Template Method
object
Abstract Factory Builder
Prototype Singleton
Adapter Bridge
Composite Decorator Facade
Flyweight Proxy
Chain of Responsibility Command
Iterator Mediator Memento Observer
State Strategy Visitor
How to learn Design Patterns?
• Joshua Kerievsky : A Learning Guide to Design Patterns
• One more time
24
message (request)
method (operation) encapsulation
signature (name, parameters, return type)
• Design: Abstraction
• Flow & Algorithms: Strategy
• State: State
• type: a specific interface
Dynamic BindingPolymorphism
Program to an interface, not an implementation
• Clients remain unaware of the specific types or class • Clients only know about the abstract class defining the interface.
Inheritance vs. Composition
• White box & Black Box • Compile time & Runtime
30
Favor object composition over class inheritance
Delegate
32
• Class composition
• Class inheritance
• Parameterized type (Generic, Template) Array<String>()
Reuse
aggregation
acquaintance(association, using)
has-A (part of)
using (know of)
Toolkit vs.
Framework
References
•
• https://s-media-cache-ak0.pinimg.com/736x/8d/dc/a5/8ddca508d43902192028cc0f0f1ad763.jpg
• https://upload.wikimedia.org/wikipedia/en/b/bf/The_Timeless_Way_of_Building.jpg
• https://d2hsbzg80yxel6.cloudfront.net/images/85778/original/9572054112.jpg
• https://images-na.ssl-images-amazon.com/images/I/81r69ARQevL.jpg
• https://d2hsbzg80yxel6.cloudfront.net/images/49583/original/9789866761799.jpg
• https://images-na.ssl-images-amazon.com/images/I/41Uat%2B4bqAL._SX258_BO1,204,203,200_.jpg
36