[디자인패턴] Bridge(브릿지) : 기능과 구현을 분리하여 독립적 변경과 확장을 용이하게 하는 패턴
디자인패턴에의 Bridge(브릿지)에 대하여 Swift를 기반으로 하여 살펴봅니다.
#. 구독 대상
- 컴퓨터 및 소프트웨어 공학과 관련자
- 소프트웨어 관련 종사자
- 기타 컴퓨터 공학에 관심이 있으신 분
- 디자인패턴의 개념을 잡고 싶으신 분
- 기타 소프트웨어 개발과 지식에 관심이 있으신 모든 분들
- Swift 언어를 활용하여 디자인패턴을 공부해보고 싶으신 분들
Bridge(브릿지)
Bridge(브릿지) 패턴은 구현부에서 추상층을 분리하여 각자 독립적으로 변형이 가능하고 확장이 가능하도록 하는 패턴입니다.
즉 기능과 구현에 대해서 두 개의 별도의 클래스로 구현을 할 수 있도록 하는 구조 설계 관련 패턴입니다.
Bridge 패턴은 객체의 다중 상속 구조를 피하면서 독립적으로 확장을 할 수 있도록 도와줍니다.
구조
Bridge(브릿지) 패턴을 UML로 도식화하면 아래와 같습니다.
Abstraction : 기능 계층의 최상위 클래스. 구현 부분에 해당하는 클래스를 인스턴스를 가지고 해당 인스턴스를 통해 구현부분의 메서드를 호출합니다.
RefindAbstraction : 기능 계층에서 새로운 부분을 확장한 클래스
Implementor : Abstraction의 기능을 구현하기 위한 인터페이스 정의
ConcreteImplementor : 실제 기능을 구현합니다.
Bridge 패턴을 사용하면 아래와 같은 다중 상속 구조를 확장이 용이하며 다중 상속을 피한 구조로 변경할 수 있습니다.
Implementation
// Implementor
protocol Car {
// implementation()
func drive()
}
// ConcreteImplementor
class Sedan : Car {
// implementation()
func drive() {
print("Drive a sedan")
}
}
// ConcreteImplementor
class SUV : Car {
func drive() {
print("Drive a SUV")
}
}
// Abstraction
protocol ColoredCar {
// implementor
var car: Car { get set }
// implementation()
func drive()
}
// RefinedAbstraction
class RedCar: ColoredCar {
// implementor
var car: Car
init(car: Car) {
self.car = car
}
// refinedFunction()
func drive() {
print("It's a red color sedan.")
// self.impl.implementation()
car.drive()
}
}
let sedan = Sedan()
let redSedan = RedCar(car: sedan)
redSedan.drive()
// It's a red color sedan.
// Drive a sedan
이상으로 Swift를 기반으로하여 Bridge(브릿지) 디자인 패턴을 설명하였습니다.
감사합니다.
www.slideshare.net/BillKim8/swift-bridge
[Swift] Bridge
Swift 소스 코드를 통한 Bridge 디자인패턴에 관한 강의 자료입니다.
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] Bridge Pattern in Swift 4 : https://medium.com/@iamcrypticcoder/bridge-pattern-in-swift-4-3472b56504b6
[2] Swift World: Design Patterns — Bridge : https://medium.com/swiftworld/swift-world-design-patterns-bridge-a20bbe999059
[3] Swift Solutions: Bridge Design Pattern : https://swiftcraft.io/swift-bridge-design-pattern/
[4] Bridge in Swift : https://refactoring.guru/design-patterns/bridge/swift/example
[5] A Design Pattern Story in Swift – Chapter 15: Bridge : http://audreyli.me/2015/07/13/a-design-pattern-story-in-swift-chapter-15-bridge/
[6] Bridge - Design patterns in Swift 2 : https://www.youtube.com/watch?v=EOkWEv8rXHk
[7] 07 브릿지 패턴 (Bridge Pattern) : https://lktprogrammer.tistory.com/35
[8] [Design Pattern] 브릿지(Bridge) 패턴 - 디자인 패턴 : https://palpit.tistory.com/197
[9] [디자인패턴] bridge & adapter & mediator pattern : https://flyburi.com/188
[10] #5 Bridge Pattern in C++(브릿지 패턴 C++) : https://vallista.tistory.com/entry/5-Bridge-Pattern-in-C-브릿지-패턴-C