Created
August 17, 2020 18:05
-
-
Save Sean-Bradley/8e379aabb406b96f7e3459fea1e543d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABCMeta, abstractmethod | |
class IA(metaclass=ABCMeta): | |
@staticmethod | |
@abstractmethod | |
def method_a(): | |
"""An abstract method A""" | |
class ClassA(IA): | |
def method_a(self): | |
print("method A") | |
class IB(metaclass=ABCMeta): | |
@staticmethod | |
@abstractmethod | |
def method_b(): | |
"""An abstract method B""" | |
class ClassB(IB): | |
def method_b(self): | |
print("method B") | |
"""ClassB does not have a method_a, so we create an adapter""" | |
class ClassBAdapter(IA): | |
def __init__(self): | |
self.class_b = ClassB() | |
def method_a(self): | |
"""calls the class b method_b instead""" | |
self.class_b.method_b() | |
# client | |
#ITEM = ClassA() | |
#ITEM = ClassB() # has no method_a | |
ITEM = ClassBAdapter() | |
ITEM.method_a() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment