SlideShare a Scribd company logo
1 of 12
Download to read offline
SWIFT
Command
Bill Kim(김정훈) | ibillkim@gmail.com
목차
•Command
•Structure
•Implementation
•References
Command
Command 패턴은 실행될 기능을 캡슐화함으로써 주어진 여러 기
능을 실행할 수 있는 재사용성이 높은 클래스를 설계하는 행위
(Behavior) 패턴입니다.
즉, 이벤트가 발생했을 때 실행될 기능이 다양하면서도 변경이 필요
한 경우에 이벤트를 발생시키는 클래스를 변경하지 않고 재사용하
고자 할 때 유용합니다.
실행될 기능을 캡슐화함으로써 기능의 실행을 요구하는 호출자
(Invoker) 클래스와 실제 기능을 실행하는 수신자(Receiver) 클래
스 사이의 의존성을 제거합니다.
따라서 실행될 기능의 변경에도 호출자 클래스를 수정 없이 그대로
사용 할 수 있도록 해줍니다.
Structure
Command 패턴을 UML로 도식화하면 아래와 같습니다.
Structure
Command : 실행될 기능에 대한 인터페이스를 선언하는 객체
ConcreteCommand : 실제로 실행되는 인터페이스를 구현하는 객
체
Invoker : 기능의 실행을 요청하는 호출자 클래스 객체
Receiver : ConcreteCommand 객체에서 실행할 메서드를 구현
할 때 필요한 클래스 객체, ConcreteCommand 객체의 기능을 실
행하기 위해서 사용하는 수신자 클래스 객체
Implementation
구체적인 구현에 대해서 소스 코드를 통하여 살펴봅니다.
protocol Command {
func execute()
}
class SimpleCommand: Command {
private var payload: String
init(_ payload: String) {
self.payload = payload
}
func execute() {
print("SimpleCommand: See, I can do simple things like printing (" +
payload + ")")
}
}
Implementation
class ComplexCommand: Command {
private var receiver: Receiver
private var a: String
private var b: String
init(_ receiver: Receiver, _ a: String, _ b: String) {
self.receiver = receiver
self.a = a
self.b = b
}
func execute() {
print("ComplexCommand: Complex stuff should be done by a receiver
object.n")
receiver.doSomething(a)
receiver.doSomethingElse(b)
}
}
Implementation
class Receiver {
func doSomething(_ a: String) {
print("Receiver: Working on (" + a + ")n")
}
func doSomethingElse(_ b: String) {
print("Receiver: Also working on (" + b + ")n")
}
}
class Invoker {
private var onStart: Command?
private var onFinish: Command?
func setOnStart(_ command: Command) {
onStart = command
}
func setOnFinish(_ command: Command) {
onFinish = command
}
func doSomethingImportant() {
onStart?.execute()
onFinish?.execute()
}
}
Implementation
let invoker = Invoker()
invoker.setOnStart(SimpleCommand("Say Hi!"))
let receiver = Receiver()
invoker.setOnFinish(ComplexCommand(receiver, "Send email", "Save report"))
// 호출자의 실행 함수를 수정을 할 필요없이 ConcreteCommand 내의 receiver 객체를 통하여 다양한 행위
를 할 수 있다.
invoker.doSomethingImportant()
// SimpleCommand: See, I can do simple things like printing (Say Hi!)
// ComplexCommand: Complex stuff should be done by a receiver object.
// Receiver: Working on (Send email)
// Receiver: Also working on (Save report)
References
[1] [Swift-Design Pattern] 커맨드 (Command pattern) : http://
throughkim.kr/2019/09/06/swift-command/
[2] [Design Pattern] 커맨드 패턴이란 : https://
gmlwjd9405.github.io/2018/07/07/command-pattern.html
[3] Design Patterns in Swift: Command Pattern : https://
agostini.tech/2018/06/03/design-patterns-in-swift-command-
pattern/
[4] Rethinking Design Patterns in Swift: https://
khawerkhaliq.com/blog/swift-design-patterns-command-
pattern/
[5] Design Patterns in Swift: Command Pattern : https://
medium.com/design-patterns-in-swift/design-patterns-in-swift-
command-pattern-b95a1f4bbc45
References
[6] Swift World: Design Patterns — Command : https://
medium.com/swiftworld/swift-world-design-patterns-command-
cc9c56544bf0
[7] [Swift] 커맨드(Command) 패턴 : https://m.blog.naver.com/
PostView.nhn?
blogId=horajjan&logNo=220804709260&proxyReferer=https:
%2F%2Fwww.google.com%2F
[8] Swift command design pattern : https://theswiftdev.com/
swift-command-design-pattern/
[9] Command in Swift : https://refactoring.guru/design-patterns/
command/swift/example
[10] [Design Pattern] 커멘드(Command) 패턴 - 디자인 패턴 : https://
palpit.tistory.com/204
Thank you!

More Related Content

Similar to [Swift] Command

Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)fefe7270
 
Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)fefe7270
 
Surface flingerservice(서피스 상태 변경 jb)
Surface flingerservice(서피스 상태 변경 jb)Surface flingerservice(서피스 상태 변경 jb)
Surface flingerservice(서피스 상태 변경 jb)fefe7270
 
Airflow를 이용한 데이터 Workflow 관리
Airflow를 이용한  데이터 Workflow 관리Airflow를 이용한  데이터 Workflow 관리
Airflow를 이용한 데이터 Workflow 관리YoungHeon (Roy) Kim
 
Android Programming
Android ProgrammingAndroid Programming
Android ProgrammingJake Yoon
 
Android Programming - Input
Android Programming - InputAndroid Programming - Input
Android Programming - InputJake Yoon
 
[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화AnselmKim
 
12장 상속 (고급)
12장 상속 (고급)12장 상속 (고급)
12장 상속 (고급)유석 남
 
Surface flingerservice(서피스 플링거 연결)
Surface flingerservice(서피스 플링거 연결)Surface flingerservice(서피스 플링거 연결)
Surface flingerservice(서피스 플링거 연결)fefe7270
 
Swift3 subscript inheritance initialization
Swift3 subscript inheritance initializationSwift3 subscript inheritance initialization
Swift3 subscript inheritance initializationEunjoo Im
 
Javascript - Function
Javascript - FunctionJavascript - Function
Javascript - Functionwonmin lee
 
20140122 techdays mini 앱 개발 세미나(3) - 센서활용 앱 개발
20140122 techdays mini  앱 개발 세미나(3) - 센서활용 앱 개발20140122 techdays mini  앱 개발 세미나(3) - 센서활용 앱 개발
20140122 techdays mini 앱 개발 세미나(3) - 센서활용 앱 개발영욱 김
 
파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229Yong Joon Moon
 
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019min woog kim
 
자바스크립트 함수
자바스크립트 함수자바스크립트 함수
자바스크립트 함수유진 변
 
Surface flingerservice(서피스 상태 변경 및 출력 요청)
Surface flingerservice(서피스 상태 변경 및 출력 요청)Surface flingerservice(서피스 상태 변경 및 출력 요청)
Surface flingerservice(서피스 상태 변경 및 출력 요청)fefe7270
 
Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)beom kyun choi
 

Similar to [Swift] Command (20)

Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)Surface flingerservice(서피스 플링거 연결 jb)
Surface flingerservice(서피스 플링거 연결 jb)
 
Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)Surface flingerservice(서피스 플링거 연결 ics)
Surface flingerservice(서피스 플링거 연결 ics)
 
Surface flingerservice(서피스 상태 변경 jb)
Surface flingerservice(서피스 상태 변경 jb)Surface flingerservice(서피스 상태 변경 jb)
Surface flingerservice(서피스 상태 변경 jb)
 
Airflow를 이용한 데이터 Workflow 관리
Airflow를 이용한  데이터 Workflow 관리Airflow를 이용한  데이터 Workflow 관리
Airflow를 이용한 데이터 Workflow 관리
 
Android Programming
Android ProgrammingAndroid Programming
Android Programming
 
Android Programming - Input
Android Programming - InputAndroid Programming - Input
Android Programming - Input
 
[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화[스프링 스터디 2일차] 서비스 추상화
[스프링 스터디 2일차] 서비스 추상화
 
12장 상속 (고급)
12장 상속 (고급)12장 상속 (고급)
12장 상속 (고급)
 
Surface flingerservice(서피스 플링거 연결)
Surface flingerservice(서피스 플링거 연결)Surface flingerservice(서피스 플링거 연결)
Surface flingerservice(서피스 플링거 연결)
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
Swift3 subscript inheritance initialization
Swift3 subscript inheritance initializationSwift3 subscript inheritance initialization
Swift3 subscript inheritance initialization
 
Javascript - Function
Javascript - FunctionJavascript - Function
Javascript - Function
 
5 swift 기초함수
5 swift 기초함수5 swift 기초함수
5 swift 기초함수
 
20140122 techdays mini 앱 개발 세미나(3) - 센서활용 앱 개발
20140122 techdays mini  앱 개발 세미나(3) - 센서활용 앱 개발20140122 techdays mini  앱 개발 세미나(3) - 센서활용 앱 개발
20140122 techdays mini 앱 개발 세미나(3) - 센서활용 앱 개발
 
파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229
 
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
 
Program team rule
Program team ruleProgram team rule
Program team rule
 
자바스크립트 함수
자바스크립트 함수자바스크립트 함수
자바스크립트 함수
 
Surface flingerservice(서피스 상태 변경 및 출력 요청)
Surface flingerservice(서피스 상태 변경 및 출력 요청)Surface flingerservice(서피스 상태 변경 및 출력 요청)
Surface flingerservice(서피스 상태 변경 및 출력 요청)
 
Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)Ji 개발 리뷰 (신림프로그래머)
Ji 개발 리뷰 (신림프로그래머)
 

More from Bill Kim

[Algorithm] Sorting Comparison
[Algorithm] Sorting Comparison[Algorithm] Sorting Comparison
[Algorithm] Sorting ComparisonBill Kim
 
[Algorithm] Big O Notation
[Algorithm] Big O Notation[Algorithm] Big O Notation
[Algorithm] Big O NotationBill Kim
 
[Algorithm] Shell Sort
[Algorithm] Shell Sort[Algorithm] Shell Sort
[Algorithm] Shell SortBill Kim
 
[Algorithm] Radix Sort
[Algorithm] Radix Sort[Algorithm] Radix Sort
[Algorithm] Radix SortBill Kim
 
[Algorithm] Quick Sort
[Algorithm] Quick Sort[Algorithm] Quick Sort
[Algorithm] Quick SortBill Kim
 
[Algorithm] Heap Sort
[Algorithm] Heap Sort[Algorithm] Heap Sort
[Algorithm] Heap SortBill Kim
 
[Algorithm] Counting Sort
[Algorithm] Counting Sort[Algorithm] Counting Sort
[Algorithm] Counting SortBill Kim
 
[Algorithm] Selection Sort
[Algorithm] Selection Sort[Algorithm] Selection Sort
[Algorithm] Selection SortBill Kim
 
[Algorithm] Merge Sort
[Algorithm] Merge Sort[Algorithm] Merge Sort
[Algorithm] Merge SortBill Kim
 
[Algorithm] Insertion Sort
[Algorithm] Insertion Sort[Algorithm] Insertion Sort
[Algorithm] Insertion SortBill Kim
 
[Algorithm] Bubble Sort
[Algorithm] Bubble Sort[Algorithm] Bubble Sort
[Algorithm] Bubble SortBill Kim
 
[Algorithm] Binary Search
[Algorithm] Binary Search[Algorithm] Binary Search
[Algorithm] Binary SearchBill Kim
 
[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)[Algorithm] Recursive(재귀)
[Algorithm] Recursive(재귀)Bill Kim
 
[Swift] Data Structure - AVL
[Swift] Data Structure - AVL[Swift] Data Structure - AVL
[Swift] Data Structure - AVLBill Kim
 
[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search Tree[Swift] Data Structure - Binary Search Tree
[Swift] Data Structure - Binary Search TreeBill Kim
 
[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)[Swift] Data Structure - Graph(BFS)
[Swift] Data Structure - Graph(BFS)Bill Kim
 
[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)[Swift] Data Structure - Graph(DFS)
[Swift] Data Structure - Graph(DFS)Bill Kim
 
[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary Tree[Swift] Data Structure - Binary Tree
[Swift] Data Structure - Binary TreeBill Kim
 
[Swift] Data Structure - Tree
[Swift] Data Structure - Tree[Swift] Data Structure - Tree
[Swift] Data Structure - TreeBill Kim
 
[Swift] Data Structure - Graph
[Swift] Data Structure - Graph[Swift] Data Structure - Graph
[Swift] Data Structure - GraphBill Kim
 

More from Bill Kim (20)

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

[Swift] Command

  • 3. Command Command 패턴은 실행될 기능을 캡슐화함으로써 주어진 여러 기 능을 실행할 수 있는 재사용성이 높은 클래스를 설계하는 행위 (Behavior) 패턴입니다. 즉, 이벤트가 발생했을 때 실행될 기능이 다양하면서도 변경이 필요 한 경우에 이벤트를 발생시키는 클래스를 변경하지 않고 재사용하 고자 할 때 유용합니다. 실행될 기능을 캡슐화함으로써 기능의 실행을 요구하는 호출자 (Invoker) 클래스와 실제 기능을 실행하는 수신자(Receiver) 클래 스 사이의 의존성을 제거합니다. 따라서 실행될 기능의 변경에도 호출자 클래스를 수정 없이 그대로 사용 할 수 있도록 해줍니다.
  • 4. Structure Command 패턴을 UML로 도식화하면 아래와 같습니다.
  • 5. Structure Command : 실행될 기능에 대한 인터페이스를 선언하는 객체 ConcreteCommand : 실제로 실행되는 인터페이스를 구현하는 객 체 Invoker : 기능의 실행을 요청하는 호출자 클래스 객체 Receiver : ConcreteCommand 객체에서 실행할 메서드를 구현 할 때 필요한 클래스 객체, ConcreteCommand 객체의 기능을 실 행하기 위해서 사용하는 수신자 클래스 객체
  • 6. Implementation 구체적인 구현에 대해서 소스 코드를 통하여 살펴봅니다. protocol Command { func execute() } class SimpleCommand: Command { private var payload: String init(_ payload: String) { self.payload = payload } func execute() { print("SimpleCommand: See, I can do simple things like printing (" + payload + ")") } }
  • 7. Implementation class ComplexCommand: Command { private var receiver: Receiver private var a: String private var b: String init(_ receiver: Receiver, _ a: String, _ b: String) { self.receiver = receiver self.a = a self.b = b } func execute() { print("ComplexCommand: Complex stuff should be done by a receiver object.n") receiver.doSomething(a) receiver.doSomethingElse(b) } }
  • 8. Implementation class Receiver { func doSomething(_ a: String) { print("Receiver: Working on (" + a + ")n") } func doSomethingElse(_ b: String) { print("Receiver: Also working on (" + b + ")n") } } class Invoker { private var onStart: Command? private var onFinish: Command? func setOnStart(_ command: Command) { onStart = command } func setOnFinish(_ command: Command) { onFinish = command } func doSomethingImportant() { onStart?.execute() onFinish?.execute() } }
  • 9. Implementation let invoker = Invoker() invoker.setOnStart(SimpleCommand("Say Hi!")) let receiver = Receiver() invoker.setOnFinish(ComplexCommand(receiver, "Send email", "Save report")) // 호출자의 실행 함수를 수정을 할 필요없이 ConcreteCommand 내의 receiver 객체를 통하여 다양한 행위 를 할 수 있다. invoker.doSomethingImportant() // SimpleCommand: See, I can do simple things like printing (Say Hi!) // ComplexCommand: Complex stuff should be done by a receiver object. // Receiver: Working on (Send email) // Receiver: Also working on (Save report)
  • 10. References [1] [Swift-Design Pattern] 커맨드 (Command pattern) : http:// throughkim.kr/2019/09/06/swift-command/ [2] [Design Pattern] 커맨드 패턴이란 : https:// gmlwjd9405.github.io/2018/07/07/command-pattern.html [3] Design Patterns in Swift: Command Pattern : https:// agostini.tech/2018/06/03/design-patterns-in-swift-command- pattern/ [4] Rethinking Design Patterns in Swift: https:// khawerkhaliq.com/blog/swift-design-patterns-command- pattern/ [5] Design Patterns in Swift: Command Pattern : https:// medium.com/design-patterns-in-swift/design-patterns-in-swift- command-pattern-b95a1f4bbc45
  • 11. References [6] Swift World: Design Patterns — Command : https:// medium.com/swiftworld/swift-world-design-patterns-command- cc9c56544bf0 [7] [Swift] 커맨드(Command) 패턴 : https://m.blog.naver.com/ PostView.nhn? blogId=horajjan&logNo=220804709260&proxyReferer=https: %2F%2Fwww.google.com%2F [8] Swift command design pattern : https://theswiftdev.com/ swift-command-design-pattern/ [9] Command in Swift : https://refactoring.guru/design-patterns/ command/swift/example [10] [Design Pattern] 커멘드(Command) 패턴 - 디자인 패턴 : https:// palpit.tistory.com/204