桥接模式(python)

from abc import ABCMeta, abstractmethod
class Shape(metaclass=ABCMeta):

    def __init__(self, color):
        self.color = color
    @abstractmethod
    def draw(self):
        pass


class Color(metaclass=ABCMeta):
    @abstractmethod
    def paint(self, shape):
        pass

class Rectangle(Shape):
    name = '长方形'
    def draw(self):
        self.color.paint(self)

class Circle(Shape):
    name = '圆'
    def draw(self):
        self.color.paint(self)

class Line(Shape):
    name = '线'
    def draw(self):
        self.color.paint(self)

class Red(Color):
    def paint(self,shape):
        print(f'红色的{shape.name}')

class Green(Color):
    def paint(self,shape):
        print(f'绿色的{shape.name}')

class Blue(Color):
    def paint(self,shape):
        print(f'蓝色的{shape.name}')

测试:



来自为知笔记(Wiz)


posted on 2021-02-14 00:44  白衣风云  阅读(65)  评论(0编辑  收藏  举报

导航