桥接模式(python)

来源:互联网 发布:python爬虫赚钱收入 编辑:程序博客网 时间:2024/05/01 03:56
桥接模式将系统抽象部分与它的实现部分分离,使它们可以独立地变化

由于目标系统存在多个角度的分类,每一种分类都会有多种变化,那么就可以把多角度分离出来,让它们独立变化减少它们之间的耦合


#encoding=utf-8##by panda#桥接模式def printInfo(info):    print unicode(info, 'utf-8').encode('gbk')#抽象类:手机品牌class HandsetBrand():    soft = None    def SetHandsetSoft(self, soft):        self.soft = soft        def Run(self):        pass    #具体抽象类:手机品牌1class HandsetBrand1(HandsetBrand):    def Run(self):        printInfo('手机品牌1:')        self.soft.Run()#具体抽象类:手机品牌2class HandsetBrand2(HandsetBrand):    def Run(self):        printInfo('手机品牌2:')        self.soft.Run()    #功能类:手机软件class HandsetSoft():    def Run(self):        pass#具体功能类:游戏    class HandsetGame(HandsetSoft):    def Run(self):        printInfo('运行手机游戏')        #具体功能类:通讯录   class HandsetAddressList(HandsetSoft):    def Run(self):        printInfo('运行手机通信录')def clientUI():    h1 = HandsetBrand1()    h1.SetHandsetSoft(HandsetAddressList())    h1.Run()    h1.SetHandsetSoft(HandsetGame())    h1.Run()        h2 = HandsetBrand2()    h2.SetHandsetSoft(HandsetAddressList())    h2.Run()    h2.SetHandsetSoft(HandsetGame())    h2.Run()        returnif __name__ == '__main__':    clientUI();

类图





原创粉丝点击