python版本的桥接模式

# -*- coding:UTF-8 -*-
from abc import ABCMeta, abstractmethod


class Implementor(metaclass=ABCMeta):
    @abstractmethod
    def operation(self):
        pass


class ConcreteImplementorA(Implementor):
    def operation(self):
        print("具体实现A的方法执行")


class ConcreteImplementorB(Implementor):
    def operation(self):
        print("具体实现B的方法执行")


class Abstraction:
    def set_implementor(self, implementor):
        self._implementor = implementor

    def operation(self):
        pass


class RefinedAbstraction(Abstraction):
    def operation(self):
        if self._implementor:
            self._implementor.operation()


if __name__ == "__main__":
    ab = RefinedAbstraction()
    ab.set_implementor(ConcreteImplementorA())
    ab.operation()
    ab.set_implementor(ConcreteImplementorB())
    ab.operation()

 

posted @ 2018-01-25 15:18  gjw  阅读(112)  评论(0)    收藏  举报