0
点赞
收藏
分享

微信扫一扫

结构型模式 6_桥模式

GhostInMatrix 2022-01-21 阅读 50

文章目录


类与类之间的复用

一个类复用另一个 类的代码的方式有:

继承; 实现类适配器; 紧耦合;

组合: 实现对象适配器; 松耦合;

在一个类中,实例化另一个类;

实例化另一个类, 从而调用该实例化后对象中的 方法

定义: 桥模式

桥模式

内容: 将一个事物的两个维度分离, 使其都可以独立的变化。

角色:

抽象; 细化抽象;
实现者;
具体实现者 ; concrete Implemetor;


一、pandas是什么?

二、 代码实例

1. 底层代码

代码如下:

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 = ' rectangle '
    def draw(self):
        # 实现长方形逻辑;
       self.color.paint(self)

class  Circle(Shape):
    name = ' circle '
    def draw(self):
        # relize the circle logic
        self.color.paint(self)



class Red(Color):
    def paint(self, shape):
        print(" red %s" % shape.name)

class Green(Color):
    def paint(self, shape):
        print("green %s " %shape.name )

2.高层代码

代码如下(示例):

shape = Rectangle(Red())
shape.draw()

shape2 = Circle(Green())
shape2.draw()


总结

提示:这里对文章进行总结:

举报

相关推荐

0 条评论