<jupyter_start><jupyter_text>桥梁模式(Bridge Pattern)   1 代码 在一个画图程序中，常会见到这样的情况：有一些预设的图形，如矩形、圆形等，还有一个对象-画笔，调节画笔的类型（如画笔还是画刷，还是毛笔效果等）并设定参数（如颜色、线宽等），选定图形，就可以在画布上画出想要的图形了。要实现以上需求，先从最抽象的元素开始设计，即形状和画笔（暂时忽略画布，同时忽略画笔参数，只考虑画笔类型）。<jupyter_code>class Shape(object):
    name=""
    param=""
    def __init__(self,*param):
        pass
    def getName(self):
        return self.name
    def getParam(self):
        return self.name,self.param

class Pen(object):
    shape=""
    type=""
    def __init__(self,shape):
        self.shape=shape
    def draw(self):
        pass<jupyter_output><empty_output><jupyter_text>形状对象和画笔对象是最为抽象的形式。接下来，构造多个形状，如矩形和圆形：<jupyter_code>class Rectangle(Shape):
    def __init__(self,long,width):
        self.name="Rectangle"
        self.param="Long:%s Width:%s"%(long,width)
        print ("Create a rectangle:%s"%self.param)
class Circle(Shape):
    def __init__(self,radius):
        self.name="Circle"
        self.param="Radius:%s"%radius
        print ("Create a circle:%s"%self.param)<jupyter_output><empty_output><jupyter_text>紧接着是构造多种画笔，如普通画笔和画刷：<jupyter_code>class NormalPen(Pen):
    def __init__(self,shape):
        Pen.__init__(self,shape)
        self.type="Normal Line"
    def draw(self):
        print ("DRAWING %s:%s----PARAMS:%s"%(self.type,self.shape.getName(),self.shape.getParam()))
class BrushPen(Pen):
    def __init__(self,shape):
        Pen.__init__(self,shape)
        self.type="Brush Line"
    def draw(self):
        print ("DRAWING %s:%s----PARAMS:%s" % (self.type,self.shape.getName(), self.shape.getParam()))
normal_pen = NormalPen(Rectangle('20cm','10cm'))
brush_pen = BrushPen(Circle('15cm'))
normal_pen.draw()
brush_pen.draw()<jupyter_output>Create a rectangle:Long:20cm Width:10cm
Create a circle:Radius:15cm
DRAWING Normal Line:Rectangle----PARAMS:('Rectangle', 'Long:20cm Width:10cm')
DRAWING Brush Line:Circle----PARAMS:('Circle', 'Radius:15cm')
{'fraction_non_alphanumeric': 0.15853018372703412, 'fraction_numerical': 0.009973753280839895, 'mean_word_length': 4.605882352941176, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '642612', 'license': 'MIT', 'n_tokens_mistral': 818, 'n_tokens_neox': 860, 'n_words': 95, 'path': 'DesignPattern/BridgePattern.ipynb', 'repo_name': 'gaufung/Data_Analytics_Learning_Note', 'size': '1905'}