객체의 구현부와 추상부를 분리하여 독립적으로 확장할 수 있도록 설계하는 구조적 디자인 패턴입니다. 이 패턴은 기능 계층과 구현 계층을 분리하여, 하나의 계층을 변경해도 다른 계층에 영향을 주지 않도록 설계합니다.
# Implementor: 색상을 정의하는 인터페이스
class Color:
def apply_color(self):
pass
# ConcreteImplementor: 색상의 구체적인 구현
class Red(Color):
def apply_color(self):
return "Red"
class Blue(Color):
def apply_color(self):
return "Blue"
# Abstraction: 모양을 정의하는 클래스
class Shape:
def __init__(self, color: Color):
self.color = color
def draw(self):
pass
# RefinedAbstraction: 모양의 구체적인 확장
class Circle(Shape):
def draw(self):
return f"Circle filled with {self.color.apply_color()} color"
class Square(Shape):
def draw(self):
return f"Square filled with {self.color.apply_color()} color"
# Client Code
red = Red()
blue = Blue()
circle = Circle(red)
square = Square(blue)
print(circle.draw()) # Output: Circle filled with Red color
print(square.draw()) # Output: Square filled with Blue color