Skip to content

Instantly share code, notes, and snippets.

@Sean-Bradley
Created August 17, 2020 18:05
Show Gist options
  • Save Sean-Bradley/8e379aabb406b96f7e3459fea1e543d4 to your computer and use it in GitHub Desktop.
Save Sean-Bradley/8e379aabb406b96f7e3459fea1e543d4 to your computer and use it in GitHub Desktop.
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