|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +class Implementation(ABC): |
| 4 | + |
| 5 | + @abstractmethod |
| 6 | + def operation_a(self) -> str: |
| 7 | + pass |
| 8 | + |
| 9 | + @abstractmethod |
| 10 | + def operation_b(self, num: int): |
| 11 | + pass |
| 12 | + |
| 13 | +class Abstraction: |
| 14 | + |
| 15 | + def __init__(self, implementation: Implementation): |
| 16 | + self.implementation = implementation |
| 17 | + |
| 18 | + def operation_one(self): |
| 19 | + self.implementation.operation_a() |
| 20 | + self.implementation.operation_b(1) |
| 21 | + |
| 22 | + def operation_two(self): |
| 23 | + self.implementation.operation_a() |
| 24 | + |
| 25 | + |
| 26 | +class ConcreteImplementationAlpha(Implementation): |
| 27 | + |
| 28 | + def operation_a(self) -> str: |
| 29 | + print("Alpha a") |
| 30 | + |
| 31 | + def operation_b(self, num: int): |
| 32 | + print("Alpha b {0}".format(num)) |
| 33 | + |
| 34 | +class ConcreteImplementationBetha(Implementation): |
| 35 | + |
| 36 | + def operation_a(self) -> str: |
| 37 | + print("Betha a") |
| 38 | + |
| 39 | + def operation_b(self, num: int): |
| 40 | + print("Betha b {0}".format(num)) |
| 41 | + |
| 42 | +class ExtendedAbstraction(Abstraction): |
| 43 | + |
| 44 | + def operation_tree(self): |
| 45 | + self.implementation.operation_b(2) |
| 46 | + |
| 47 | +def main(): |
| 48 | + abstractions = [Abstraction(ConcreteImplementationAlpha()), |
| 49 | + Abstraction(ConcreteImplementationBetha())] |
| 50 | + |
| 51 | + for abstraction in abstractions: |
| 52 | + abstraction.operation_one() |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + main() |
0 commit comments