Design Patterns in Swift ch0 Introduction

36
Design Patterns in Swift Ch 0 Introduction Sai Li @ Yowoo Tech. 2017/3/17

Transcript of Design Patterns in Swift ch0 Introduction

Page 1: Design Patterns in Swift ch0 Introduction

Design Patterns in Swift

Ch 0 Introduction

Sai Li @ Yowoo Tech. 2017/3/17

Page 2: Design Patterns in Swift ch0 Introduction

Agenda

• Prologue • What is Pattern? • OOP Recap

2

Page 3: Design Patterns in Swift ch0 Introduction

• After OOP • Clean Code, OOP, Design Pattern,

Refactor, Platform Framework, Test • Future interview

3

know nothingwork

Page 4: Design Patterns in Swift ch0 Introduction

What is a Pattern?

Page 5: Design Patterns in Swift ch0 Introduction

Christopher Alexander (1936 - )

Page 6: Design Patterns in Swift ch0 Introduction

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)

Page 7: Design Patterns in Swift ch0 Introduction

Pattern is a solution to a problem in a context

Page 8: Design Patterns in Swift ch0 Introduction

• 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

Page 9: Design Patterns in Swift ch0 Introduction

Pattern is a solution to a recurring problem in a context

Page 10: Design Patterns in Swift ch0 Introduction

• 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.

Page 11: Design Patterns in Swift ch0 Introduction

Pattern is a proven solution to a recurring problem in a context

Page 12: Design Patterns in Swift ch0 Introduction

• 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…

Page 13: Design Patterns in Swift ch0 Introduction

Pattern is a proven solution to a recurring problem in a

specific context

Page 14: Design Patterns in Swift ch0 Introduction

• 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 …

Page 15: Design Patterns in Swift ch0 Introduction

Pattern is a proven solution to a recurring problem in a specific context

Page 16: Design Patterns in Swift ch0 Introduction

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

Page 17: Design Patterns in Swift ch0 Introduction

17

• Name • Context • Problem • Solution • Force • Resulting Context

ContextProblem

Elements of Pattern

Solution(Form)Force

Page 18: Design Patterns in Swift ch0 Introduction

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

Page 19: Design Patterns in Swift ch0 Introduction
Page 20: Design Patterns in Swift ch0 Introduction

20

Erich Gamma, Richard Helm,

Ralph Johnson, John Vlissides

Gang of Four (GoF)1994 ~

Page 21: Design Patterns in Swift ch0 Introduction

References

by Teddy Chen

21

Page 22: Design Patterns in Swift ch0 Introduction

• Purpose 1. Creational 2. Structural 3. Behavioral

22

• Scope 1. Class: static 2. Object: dynamic

Page 23: Design Patterns in Swift ch0 Introduction

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

Page 24: Design Patterns in Swift ch0 Introduction

How to learn Design Patterns?

• Joshua Kerievsky : A Learning Guide to Design Patterns

• One more time

24

Page 25: Design Patterns in Swift ch0 Introduction

message (request)

method (operation) encapsulation

signature (name, parameters, return type)

Page 26: Design Patterns in Swift ch0 Introduction

• Design: Abstraction

• Flow & Algorithms: Strategy

• State: State

Page 27: Design Patterns in Swift ch0 Introduction

• type: a specific interface

Dynamic BindingPolymorphism

Page 28: Design Patterns in Swift ch0 Introduction
Page 29: Design Patterns in Swift ch0 Introduction

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.

Page 30: Design Patterns in Swift ch0 Introduction

Inheritance vs. Composition

• White box & Black Box • Compile time & Runtime

30

Page 31: Design Patterns in Swift ch0 Introduction

Favor object composition over class inheritance

Page 32: Design Patterns in Swift ch0 Introduction

Delegate

32

Page 33: Design Patterns in Swift ch0 Introduction

• Class composition

• Class inheritance

• Parameterized type (Generic, Template) Array<String>()

Reuse

Page 34: Design Patterns in Swift ch0 Introduction

aggregation

acquaintance(association, using)

has-A (part of)

using (know of)

Page 35: Design Patterns in Swift ch0 Introduction

Toolkit vs.

Framework

Page 36: Design Patterns in Swift ch0 Introduction

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