ݺߣ

ݺߣShare a Scribd company logo
Design Patterns
in Swift
Ch 1
Creational Patterns
Simple Factory
Factory Method
Sai Li @ Yowoo Tech.
taipingeric@gmail.com
2017/4/25
Agenda
? Creational Patterns
? Simple Factory
? Factory Method
2
References
ЦՄܛ? ?
by Teddy Chen
3
?һ}?Ա
1. Factory Method
2. Strategy
3. Decorator
4. Composite
5. Iterator
6. Template Method
7. Abstract Factory
8. Builder
9. Singleton
10. Proxy
11. Adaptor
Kerievsky Joshua: A Learning Guide To Design Patterns
12. Bridge
13. Mediator
14. Observer
15. Chain of Responsibility
16. Memento
17. Command
18. Prototype
19. State
20. Visitor
21. Flyweight
22. Interpreter
23. Facade
Kerievsky Joshua: A Learning Guide To Design Patterns
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
OOP
? DIP: depends on interface
6
? Swift
? let myPhone = Phone()
? Java
? Phone myPhone = new Phone()
? Javascript
? var myPhone = new Phone()
init()
Creational Patterns
? Abstracting initialization process
? Theme
1. Encapsulate concrete class knowledge
2. Hiding instance creation and composition by
knowing interface only
3. Flexibility: What, Who, How, When
4. static -> compiling time, dynamic -> runtime
8
Simple Factory
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Design Patterns in Swift Ch1 Factory Method
Factory Method
Factory Method
De?ne an interface for creating an object, but let
subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to
subclasses.
16
x?Ľ?棬׌?eQԓw
?һN
Factory Methodʹew(t?)?e
̎
Product Creator
created objects interface
concrete implementation
1.de?ne Factory Method Interface
(2. default implementation)
override Factory Method
to return ConcreteProduct

let product = FactoryMethod()

return ConcreteProduct()
<< create >>
Design Patterns in Swift Ch1 Factory Method
factory method
Design Patterns in Swift Ch1 Factory Method
extension
Simple Factory ?
vs.?
Factory Method
w/o default implementation
References
? ЦՄܛ?
? Kerievsky Joshua: A Learning Guide To Design Patterns
26
Q & A

More Related Content

Design Patterns in Swift Ch1 Factory Method