250x250
반응형
05-21 00:05
Today
Total
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Notice
Recent Posts
Recent Comments
Link
Archives
관리 메뉴

Bill Kim's Life...

[디자인패턴] Command(커맨드) : 실행될 기능을 캡슐화하여 여러 기능을 실행 본문

CS(컴퓨터 과학)/Design Patterns

[디자인패턴] Command(커맨드) : 실행될 기능을 캡슐화하여 여러 기능을 실행

billnjoyce 2020. 6. 12. 16:37
728x90
반응형
디자인패턴에의 Command(커맨드)에 대하여 Swift를 기반으로 하여 살펴봅니다.

 

 

#. 구독 대상

  • 컴퓨터 및 소프트웨어 공학과 관련자
  • 소프트웨어 관련 종사자 
  • 기타 컴퓨터 공학에 관심이 있으신 분
  • 디자인패턴의 개념을 잡고 싶으신 분
  • 기타 소프트웨어 개발과 지식에 관심이 있으신 모든 분들
  • Swift 언어를 활용하여 디자인패턴을 공부해보고 싶으신 분들

 

 


 

Command(커맨드)

 

Command 패턴은 실행될 기능을 캡슐화함으로써 주어진 여러 기능을 실행할 수 있는 재사용성이 높은 클래스를 설계하는 행위(Behavior) 패턴입니다.

 

즉, 이벤트가 발생했을 때 실행될 기능이 다양하면서도 변경이 필요한 경우에 이벤트를 발생시키는 클래스를 변경하지 않고 재사용하고자 할 때 유용합니다.

 

실행될 기능을 캡슐화함으로써 기능의 실행을 요구하는 호출자(Invoker) 클래스와 실제 기능을 실행하는 수신자(Receiver) 클래스 사이의 의존성을 제거합니다.

 

따라서 실행될 기능의 변경에도 호출자 클래스를 수정 없이 그대로 사용 할 수 있도록 해줍니다.

 

 

 

 

 


 

 

 

구조

 

Command(커맨드) 패턴을 UML로 도식화하면 아래와 같습니다. 

 

 

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 + ")")
    }
}

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)
    }
}

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()
    }
}
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)

 

 

 

 


 

 

 

이상으로 Swift를 기반으로하여 Command(커맨드) 디자인 패턴을 설명하였습니다.

 

 

 

 

감사합니다.

 

 

 

 

 

 

www.slideshare.net/BillKim8/swift-command

 

[Swift] Command

Swift 소스 코드를 통한 Command 디자인패턴에 관한 강의 자료입니다.

www.slideshare.net

github.com/billnjoyce/Lectures/tree/master/src/designpatterns

 

billnjoyce/Lectures

Contribute to billnjoyce/Lectures development by creating an account on GitHub.

github.com

 

 

 


[참고 자료(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

[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

728x90
반응형
Comments