python设计模式之桥接模式
来源:互联网 发布:功率串联和并联的算法 编辑:程序博客网 时间:2024/05/22 08:01
桥接模式
这个模式其实就是把产品类的实现和抽象类分离,能够灵活的变化,假如你记得状态模式,它是修改内部属性, 而桥接模式是指定好内部属性,每个产品类指定这个属性被桥接模式类调用,适用于产品类可能经常调整变化,这样还能减少了产品类之间的耦合
python的例子
这里实现一个打印操作系统名字的功能
class Bridge(object): def __init__(self): self.__implementation = None def someFunctionality(self): raise NotImplemented()class UseCase1(Bridge): # 根据初始化参数传入实现的产品类 def __init__(self, implementation): self.__implementation = implementation # 根据传入的产品类的属性打印结果 def someFunctionality(self): print "UseCase1: ", self.__implementation.anotherFunctionality()class UseCase2(Bridge): def __init__(self, implementation): self.__implementation = implementation def someFunctionality(self): print "UseCase2: ", self.__implementation.anotherFunctionality()class ImplementationInterface: def anotherFunctionality(self): raise NotImplemented# 这里其实才是实现的产品类class Linux(ImplementationInterface): # 它定义了这个方法,回应操作系统的名字 def anotherFunctionality(self): print "Linux!"class Windows(ImplementationInterface): def anotherFunctionality(self): print "Windows."def main(): linux = Linux() windows = Windows() useCase = UseCase1(linux) useCase.someFunctionality() useCase = UseCase1(windows) useCase.someFunctionality() useCase = UseCase2(linux) useCase.someFunctionality() useCase = UseCase2(windows) useCase.someFunctionality()if __name__ == "__main__": main()
0 0
- python设计模式之桥接模式
- python设计模式之桥接模式
- Python的结构型设计模式之桥接模式
- 设计模式之桥接模式Python实现
- Python设计模式(四)【桥接模式】
- Python设计模式-桥接模式
- python设计模式之设计模式简介
- python设计模式之状态设计模式
- python设计模式 之 简单工厂模式
- python 设计模式之 单例模式
- python设计模式之访问者模式
- python设计模式之备忘录模式
- python设计模式之对象池模式
- python设计模式之代理模式
- python设计模式之模板方法模式
- python设计模式之Null模式
- python设计模式之组合模式
- python设计模式之责任链模式
- 【HttpClient4.5中文教程】【第一章 :基础】1.1执行请求(一)
- 函数柯里化or not
- hdu 2120 Ice_cream's world I
- 输出等腰三角形的*
- JAVA基础点汇总(三)
- python设计模式之桥接模式
- Struts2 的ModelDriven 一 —— 传值
- Incorrect string value: '' for column '' at row 1
- android开发的小知识--选择编译
- 畅通工程
- hdu 3473 Minimum Sum
- Opengl 绘图出现白板、像素不清晰的问题解决办法
- urllib2使用总结
- source、sh、bash、./执行脚本的区别