250x250
반응형
05-21 03:08
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...

[디자인패턴] Decorator(데코레이터) : 객체에 새로운 기능을 동적으로 추가 본문

CS(컴퓨터 과학)/Design Patterns

[디자인패턴] Decorator(데코레이터) : 객체에 새로운 기능을 동적으로 추가

billnjoyce 2020. 6. 12. 16:14
728x90
반응형
디자인패턴에의 Decorator(데코레이터)에 대하여 Swift를 기반으로 하여 살펴봅니다.

 

 

#. 구독 대상

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

 

 


 

Decorator(데코레이터)

 

Decorator(데코레이터) 디자인 패턴은 특정 객체에 대해서 새로운 기능을 동적(Run-time)으로 추가하기 위한 구조 설계 패턴입니다.

 

객체에 동적으로 기능을 추가하고 코드의 추가를 숨기고 싶은 경우 사용하면 좋습니다.

 

또한 상속을 통해서 객체를 확장할 수 없는 경우에서도 사용할 수 있습니다.

 

 

 

 

 


 

 

 

구조

 

Decorator 패턴을 UML로 도식화하면 아래와 같습니다. 

 

 

 

Component : 기능을 동적으로 확장하기위해 기본이 되는 추상 클래스 객체

ConcreteComponent : 컴포넌트(Component) 객체의 인터페이스를 구체화시키는 객체

Decorator : Component 객체를 소유하며 Component의 인터페이스를 실행하는 객체

ConcreteDecorator : Decorator 가 가지고 있는 Component 객체에 대한 인터페이스의 구체화를 실현하는 객체

 

 

 

 

 


 

 

 

 

Implementation

 

protocol Component {
    func operation() -> String
}

class ConcreteComponent: Component {
    func operation() -> String {
        return "ConcreteComponent operation"
    }
}

class Decorator : Component {
    private var component: Component

    init(_ component: Component) {
        self.component = component
    }

    func operation() -> String {
        return component.operation()
    }
}

class ConcreteDecoratorA : Decorator {
    override func operation() -> String {
        return "ConcreteDecoratorA(" + super.operation() + ")"
    }
}

class ConcreteDecoratorB : Decorator {
    override func operation() -> String {
        return "ConcreteDecoratorB(" + super.operation() + ")"
    }
}
let component = ConcreteComponent()
print("Result: " + component.operation()) 
// Result: ConcreteComponent operation
        
let decorator1 = ConcreteDecoratorA(component)
print("Result: " + decorator1.operation()) 
// Result: ConcreteDecoratorA(ConcreteComponent operation)
        
let decorator2 = ConcreteDecoratorB(decorator1)
print("Result: " + decorator2.operation()) 
// Result: ConcreteDecoratorB(ConcreteDecoratorA(ConcreteComponent operation))

 

 

 

 


 

 

 

이상으로 Swift를 기반으로하여 Decorator(데코레이터) 디자인 패턴을 설명하였습니다.

 

 

 

 

감사합니다.

 

 

 

 

 

www.slideshare.net/BillKim8/swift-decorator

 

[Swift] Decorator

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

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] 데코레이터 패턴(Decorator pattern) : http://throughkim.kr/2019/09/09/swift-decorator/

[2] Decorator in Swift : https://refactoring.guru/design-patterns/decorator/swift/example#lang-features

[3] [DesignPattern]데코레이터 패턴(Decorator Pattern) : http://minsone.github.io/programming/designpattern-decorator

[4] Top 5 스위프트 디자인 패턴 (번역) : https://leejigun.github.io/Top_5_Design_Patterns

[5] Design Patterns in Swift: Decorator Pattern : https://medium.com/design-patterns-in-swift/design-patterns-in-swift-decorator-pattern-2026e7112869

[6] Decorator pattern in Swift : https://medium.com/jeremy-codes/decorator-pattern-in-swift-e5fa11ea3c3f

[7] Swift Solutions: Decorator Pattern : https://swiftcraft.io/decorator-pattern-swift/

[8] [Design Pattern] 데코레이터(Decorator) 패턴 - 디자인 패턴 : https://palpit.tistory.com/193

[9] 데코레이터 패턴(Decorator Pattern) : https://unabated.tistory.com/entry/데코레이터-패턴Decorator-Pattern

[10] Design Patterns on iOS using Swift – Part 1/2 : https://www.raywenderlich.com/477-design-patterns-on-ios-using-swift-part-1-2

728x90
반응형
Comments