桥接模式:将抽象和实现解耦,使他们可以独立的变化
桥接模式是结构型模式,侧重于软件结构
from abc import ABCMeta,abstractmethod
class Shape(metaclass=ABCMeta):
def __init__(self,color):
self.__color = color
@abstractmethod
def getShapeType(self):
pass
def showShapeInfo(self):
print( self.__color.getColor() + "的" + self.getShapeType())
class Rect(Shape):
def __init__(self,color):
super().__init__(color)
def getShapeType(self):
return "矩形"
class Ellipse(Shape):
def __init__(self,color):
super().__init__(color)
def getShapeType(self):
return "椭圆"
class Color(metaclass=ABCMeta):
@abstractmethod
def getColor(self):
pass
class Red(Color):
def getColor(self):
return "红色"
class Blue(Color):
def getColor(self):
return "蓝色"
def test():
redRect = Rect(Red())
redRect.showShapeInfo();
blueRect = Rect(Blue())
blueRect.showShapeInfo()
redEllipse = Ellipse(Red())
redEllipse.showShapeInfo()
test()
运行结果:
红色的矩形
蓝色的矩形
红色的椭圆
aaa