Bridge mode
Apply to : Several kinds , A design pattern for any combination
from abc import ABCMeta, abstractmethod
# shape
class Shape(metaclass=ABCMeta):
# When using shapes You need to input a color before setting Bridge mode Bridge
# In actual use, two objects are passed in , One is the input object , The other is its own object
# The input object is assigned to color
def __init__(self, color):
self.color = color
@abstractmethod
def draw(self):
pass
# Color
class Color(metaclass=ABCMeta):
@abstractmethod
def paint(self, shape):
pass
class Re(Shape):
name = " square "
def draw(self):
# The key is this step
# In actual use, two objects are passed in , One is the input object , The other is its own object
# The input object is assigned to color
# What we need to call is Input object Methods .paint()
# self Pass yourself as a parameter to self.color.paint() This function Dolls belong to
self.color.paint(self)
class Red(Color):
def paint(self, shape):
print(" Red ", shape.name)
# You can choose to shape ( Color )
# By expanding New shape class And Color class To enrich the combination
# This is it. Bridge mode
s = Re(Red())
s.draw()