程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python design pattern bridge pattern

編輯:Python

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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved