Python中桥模式的设计模式是如何实现的?

2026-05-16 15:511阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计637个文字,预计阅读时间需要3分钟。

Python中桥模式的设计模式是如何实现的?

增加信息,参考[设计模式],桥模式内容:将一个事物的两个维度分离,使其可独立变化。桥模式中的角色包括:桥接角色、抽象角色、细化抽象角色、实现者、整体。


更多信息请参考

桥模式内容

将一个事务的两个维度分离,使其都可以独立地变化

桥模式中的角色

  • 抽象(Abstraction)
  • 细化抽象(Refined Abstraction)
  • 实现者(Implementor)
  • 具体实现者(concrete Implementor)

桥模式的使用场景

  • 当事务有两个维度上的表现,两个维度都有可能扩展时

桥模式的优点

  • 抽象和实现相分离
  • 优秀的扩展能力

桥模式实例

(1)未使用桥模式时,类设计如下,即形状和颜色两个维度都变化时,类的数量是很多的

Python中桥模式的设计模式是如何实现的?

class Shape:
pass

class Line(Shape):
pass

class Rectangle(Shape):
pass

class Circle(Shape):
pass

class RedLine(Line):
pass

class GreenLine(Line):
pass

class BlueLine(Line):
pass

class RedRectangle(Rectangle):
pass

class GreeRectangle(Rectangle):
pass

class BlueRectangel(Rectangle):
pass

class RedCircle(Circle):
pass

class GreenCircle(Circle):
pass

class BlueCircle(Circle):
pass

(2)使用桥模式时,类代码如下:

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 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}")

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)

if __name__=="__main__":
shap1=Rectangle(Red())
shap1.draw()

shap2=Circle(Blue())
shap2.draw()

shap3=Line(Green())
shap3.draw()

执行结果如下:

红色的长方形
蓝色的圆形
绿色的直线

如此,则颜色和形状两个维度可以自由变化,然后进行自由组合即可完成不同颜色的不同形状

本文共计637个文字,预计阅读时间需要3分钟。

Python中桥模式的设计模式是如何实现的?

增加信息,参考[设计模式],桥模式内容:将一个事物的两个维度分离,使其可独立变化。桥模式中的角色包括:桥接角色、抽象角色、细化抽象角色、实现者、整体。


更多信息请参考

桥模式内容

将一个事务的两个维度分离,使其都可以独立地变化

桥模式中的角色

  • 抽象(Abstraction)
  • 细化抽象(Refined Abstraction)
  • 实现者(Implementor)
  • 具体实现者(concrete Implementor)

桥模式的使用场景

  • 当事务有两个维度上的表现,两个维度都有可能扩展时

桥模式的优点

  • 抽象和实现相分离
  • 优秀的扩展能力

桥模式实例

(1)未使用桥模式时,类设计如下,即形状和颜色两个维度都变化时,类的数量是很多的

Python中桥模式的设计模式是如何实现的?

class Shape:
pass

class Line(Shape):
pass

class Rectangle(Shape):
pass

class Circle(Shape):
pass

class RedLine(Line):
pass

class GreenLine(Line):
pass

class BlueLine(Line):
pass

class RedRectangle(Rectangle):
pass

class GreeRectangle(Rectangle):
pass

class BlueRectangel(Rectangle):
pass

class RedCircle(Circle):
pass

class GreenCircle(Circle):
pass

class BlueCircle(Circle):
pass

(2)使用桥模式时,类代码如下:

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 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}")

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)

if __name__=="__main__":
shap1=Rectangle(Red())
shap1.draw()

shap2=Circle(Blue())
shap2.draw()

shap3=Line(Green())
shap3.draw()

执行结果如下:

红色的长方形
蓝色的圆形
绿色的直线

如此,则颜色和形状两个维度可以自由变化,然后进行自由组合即可完成不同颜色的不同形状