Bridge mode
Overview
- Separate the two dimensions of a thing so that it can change independently
Object
- Abstraction
- Refine Abstraction (RefindAbstraction)
- Implementor
- Concrete implementer (ConcreteImplementor)
Applicable scene
- When things have performance in two dimensions, and it is possible to expand in each dimension
example
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 Circular(Shape):
name = "圆"
def draw(self):
self.color.paint(self)
class Red(Color):
name = "红色"
def paint(self, shape):
print(f"{Red.name}的{shape.name}")
class Blue(Color):
name = "蓝色"
def paint(self, shape):
print(f"{Blue.name}的{shape.name}")
rectangle = Rectangle(Red())
rectangle.draw()
advantage
- Separation of abstraction and realization
- Excellent scalability