SlideShare a Scribd company logo
SWIFT
Prototype
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Prototype
•Structure
•Implementation
•References
Prototype
프로토타입(Prototype) 디자인 패턴은 객체 생성을 위한 패턴으로
서 클래스 정의와 생성 방식을 구조화 및 캡슐화하여 수행할 수 있
도록 도와주는 디자인 패턴입니다.
프로토타입 패턴은 원형이 되는 인스턴스를 사용하여 생성할 객체
의 종류를 명시하고, 이렇게 만든 견본을 복사해서 새롭게 객체를 생
성하여 사용하는 패턴입니다.
구체 클래스를 알 수 없는 경우에도 객체를 복사할 수 있는 공통된
인터페이스를 제공합니다.
동일한 클래스의 객체는 내부 프로퍼티를 모두 알 수 있으므로 프로
토타입 객체는 전체 복사본을 생성할 수 있습니다.
Prototype
프로토타입의 주요 구조를 요약하면 아래와 같습니다.
1. 공통의 인터페이스를 갖는 Prototype 객체를 선언합니다.
2. Prototype 객체의 clone 함수를 통하여 새로운
ConcreteProtype 객체를 생성하여 사용할 수 있습니다.
3. 새로운 ConcreteProtype 객체가 추가되더라도 Client 객체
는 수정될 필요가 없습니다.
Structure
크게 구조를 보면 아래와 같은 구조를 같습니다.
Prototype : 객체 복제(Clone)를 위한 원형 인터페이스 객체
ConcretePrototype : 자신을 복제하는 객체
Client : 실제적으로 복제를 요청하고 사용할 객체
Implementation
구체적인 구현에 대해서 소스 코드를 통하여 살펴봅니다.
protocol Prototype {
func operation(value: Int)
func clone() -> Prototype
}
class ConcretePrototypeA : Prototype {
init() { }
init(client: ConcretePrototypeA) { }
func operation(value: Int) { print("ConcretePrototypeA operation - value : (value)") }
func clone() -> Prototype {
print("ConcretePrototypeA clone")
return ConcretePrototypeA(client: self)
}
}
class ConcretePrototypeB : Prototype {
init() { }
init(client: ConcretePrototypeB) { }
func operation(value: Int) { print("ConcretePrototypeB operation - value : (value*2)") }
func clone() -> Prototype {
print("ConcretePrototypeB clone")
return ConcretePrototypeB(client: self)
}
}
Implementation
func concretePrototype(prototype: Prototype) -> Prototype
{
// Client는 구체적인 ConcretePrototype 객체 접근 불필요
// 모든 ConcretePrototype 객체에 대해 알 필요가 없다.
// 새로운 ConcretePrototype 객체가 추가되더라도 객체 생성 시 수정이 불필요
let newPrototype = prototype.clone()
return newPrototype
}
let clientA = concretePrototype(prototype: ConcretePrototypeA())
clientA.operation(value: 10)
// ConcretePrototypeA clone
// ConcretePrototypeA operation - value : 10
let clientB = concretePrototype(prototype: ConcretePrototypeB())
clientB.operation(value: 10)
// ConcretePrototypeB clone
// ConcretePrototypeB operation - value : 20
References
[1] Prototype pattern in Swift : https://medium.com/
jeremy-codes/prototype-pattern-in-swift-1b50517d1075
[2] 프로토타입 패턴 (Prototype Pattern in Swift) : https://
jerome.kr/entry/prototype-pattern?category=1114713
[3] Swift prototype design pattern : https://
theswiftdev.com/swift-prototype-design-pattern/
[4] Prototype in Swift : https://refactoring.guru/
design-patterns/prototype/swift/example
[5] PatternPrototype : https://github.com/qbbang/
PatternPrototype
References
[6] [소프트위어 공학 - Prototype Pattern] : https://
m.blog.naver.com/scw0531/221465259625
[7] [Design Pattern] 프로토타입(Prototype) 패턴 - 디자인 패
턴 : https://palpit.tistory.com/189
[8] DesignPattern : 프로토타입 (ProtoType) : http://
egloos.zum.com/EireneHue/v/976506
[9] 프로토타입 패턴 : https://ko.wikipedia.org/wiki/프로토타입
_패턴
[10] Design Patterns in Swift: Prototype Pattern : https://
swiftscheming.wordpress.com/2016/04/17/design-patterns-
in-swift-prototype-pattern/
Thank you!

More Related Content

PPTX
Builder, prototype, singleton pattern
PDF
[Swift] Template Method
PDF
Gpg 1.1
PDF
Javascript - Function
PDF
Javascript 객체생성패턴
PPTX
Javascript closure 2차과제 이승찬
PDF
typeofnull이 object인 eu
PDF
Effective unit testing - 좋은테스트 요약
Builder, prototype, singleton pattern
[Swift] Template Method
Gpg 1.1
Javascript - Function
Javascript 객체생성패턴
Javascript closure 2차과제 이승찬
typeofnull이 object인 eu
Effective unit testing - 좋은테스트 요약

Similar to [Swift] Prototype (20)

PDF
HolubOnPatterns/chapter2_2
ODP
Design Pattern 3
PDF
[Swift] Abstract Factory
PPTX
Design patterns
PPTX
Api design for c++ ch3 pattern
PPTX
Api design for c++ pattern
PDF
GPG 1.1 객체지향적 프로그래밍과 설계기법
PPT
Proxy pattern
PPTX
Refelction의 개념과 RTTR 라이브러리
PPTX
09장 객체와 클래스 (고급)
PPTX
Java script의 이해
PPTX
Design patterns
 
PDF
[Swift] Factory Method
PDF
08장 객체와 클래스 (기본)
PDF
9 object class
PDF
[Swift] Builder
PDF
GoF_(Gang of Four)_디자인_패턴_20241115서만원.pdf
PDF
[Swift] Proxy
PDF
PPTX
모어 이펙티브 c++ 5장 스터디
HolubOnPatterns/chapter2_2
Design Pattern 3
[Swift] Abstract Factory
Design patterns
Api design for c++ ch3 pattern
Api design for c++ pattern
GPG 1.1 객체지향적 프로그래밍과 설계기법
Proxy pattern
Refelction의 개념과 RTTR 라이브러리
09장 객체와 클래스 (고급)
Java script의 이해
Design patterns
 
[Swift] Factory Method
08장 객체와 클래스 (기본)
9 object class
[Swift] Builder
GoF_(Gang of Four)_디자인_패턴_20241115서만원.pdf
[Swift] Proxy
모어 이펙티브 c++ 5장 스터디
Ad

More from Bill Kim (20)

PDF
[Algorithm] Sorting Comparison
PDF
[Algorithm] Big O Notation
PDF
[Algorithm] Shell Sort
PDF
[Algorithm] Radix Sort
PDF
[Algorithm] Quick Sort
PDF
[Algorithm] Heap Sort
PDF
[Algorithm] Counting Sort
PDF
[Algorithm] Selection Sort
PDF
[Algorithm] Merge Sort
PDF
[Algorithm] Insertion Sort
PDF
[Algorithm] Bubble Sort
PDF
[Algorithm] Binary Search
PDF
[Algorithm] Recursive(재귀)
PDF
[Swift] Data Structure - AVL
PDF
[Swift] Data Structure - Binary Search Tree
PDF
[Swift] Data Structure - Graph(BFS)
PDF
[Swift] Data Structure - Graph(DFS)
PDF
[Swift] Data Structure - Binary Tree
PDF
[Swift] Data Structure - Tree
PDF
[Swift] Data Structure - Graph
[Algorithm] Sorting Comparison
[Algorithm] Big O Notation
[Algorithm] Shell Sort
[Algorithm] Radix Sort
[Algorithm] Quick Sort
[Algorithm] Heap Sort
[Algorithm] Counting Sort
[Algorithm] Selection Sort
[Algorithm] Merge Sort
[Algorithm] Insertion Sort
[Algorithm] Bubble Sort
[Algorithm] Binary Search
[Algorithm] Recursive(재귀)
[Swift] Data Structure - AVL
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Tree
[Swift] Data Structure - Graph
Ad

[Swift] Prototype

  • 3. Prototype 프로토타입(Prototype) 디자인 패턴은 객체 생성을 위한 패턴으로 서 클래스 정의와 생성 방식을 구조화 및 캡슐화하여 수행할 수 있 도록 도와주는 디자인 패턴입니다. 프로토타입 패턴은 원형이 되는 인스턴스를 사용하여 생성할 객체 의 종류를 명시하고, 이렇게 만든 견본을 복사해서 새롭게 객체를 생 성하여 사용하는 패턴입니다. 구체 클래스를 알 수 없는 경우에도 객체를 복사할 수 있는 공통된 인터페이스를 제공합니다. 동일한 클래스의 객체는 내부 프로퍼티를 모두 알 수 있으므로 프로 토타입 객체는 전체 복사본을 생성할 수 있습니다.
  • 4. Prototype 프로토타입의 주요 구조를 요약하면 아래와 같습니다. 1. 공통의 인터페이스를 갖는 Prototype 객체를 선언합니다. 2. Prototype 객체의 clone 함수를 통하여 새로운 ConcreteProtype 객체를 생성하여 사용할 수 있습니다. 3. 새로운 ConcreteProtype 객체가 추가되더라도 Client 객체 는 수정될 필요가 없습니다.
  • 5. Structure 크게 구조를 보면 아래와 같은 구조를 같습니다. Prototype : 객체 복제(Clone)를 위한 원형 인터페이스 객체 ConcretePrototype : 자신을 복제하는 객체 Client : 실제적으로 복제를 요청하고 사용할 객체
  • 6. Implementation 구체적인 구현에 대해서 소스 코드를 통하여 살펴봅니다. protocol Prototype { func operation(value: Int) func clone() -> Prototype } class ConcretePrototypeA : Prototype { init() { } init(client: ConcretePrototypeA) { } func operation(value: Int) { print("ConcretePrototypeA operation - value : (value)") } func clone() -> Prototype { print("ConcretePrototypeA clone") return ConcretePrototypeA(client: self) } } class ConcretePrototypeB : Prototype { init() { } init(client: ConcretePrototypeB) { } func operation(value: Int) { print("ConcretePrototypeB operation - value : (value*2)") } func clone() -> Prototype { print("ConcretePrototypeB clone") return ConcretePrototypeB(client: self) } }
  • 7. Implementation func concretePrototype(prototype: Prototype) -> Prototype { // Client는 구체적인 ConcretePrototype 객체 접근 불필요 // 모든 ConcretePrototype 객체에 대해 알 필요가 없다. // 새로운 ConcretePrototype 객체가 추가되더라도 객체 생성 시 수정이 불필요 let newPrototype = prototype.clone() return newPrototype } let clientA = concretePrototype(prototype: ConcretePrototypeA()) clientA.operation(value: 10) // ConcretePrototypeA clone // ConcretePrototypeA operation - value : 10 let clientB = concretePrototype(prototype: ConcretePrototypeB()) clientB.operation(value: 10) // ConcretePrototypeB clone // ConcretePrototypeB operation - value : 20
  • 8. References [1] Prototype pattern in Swift : https://medium.com/ jeremy-codes/prototype-pattern-in-swift-1b50517d1075 [2] 프로토타입 패턴 (Prototype Pattern in Swift) : https:// jerome.kr/entry/prototype-pattern?category=1114713 [3] Swift prototype design pattern : https:// theswiftdev.com/swift-prototype-design-pattern/ [4] Prototype in Swift : https://refactoring.guru/ design-patterns/prototype/swift/example [5] PatternPrototype : https://github.com/qbbang/ PatternPrototype
  • 9. References [6] [소프트위어 공학 - Prototype Pattern] : https:// m.blog.naver.com/scw0531/221465259625 [7] [Design Pattern] 프로토타입(Prototype) 패턴 - 디자인 패 턴 : https://palpit.tistory.com/189 [8] DesignPattern : 프로토타입 (ProtoType) : http:// egloos.zum.com/EireneHue/v/976506 [9] 프로토타입 패턴 : https://ko.wikipedia.org/wiki/프로토타입 _패턴 [10] Design Patterns in Swift: Prototype Pattern : https:// swiftscheming.wordpress.com/2016/04/17/design-patterns- in-swift-prototype-pattern/