橋接模式:將抽象和實現解耦,使他們可以獨立的變化
橋接模式是結構型模式,側重於軟件結構
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