使用python中的抽象方法设置委托设计模式

时间:2017-10-10 22:54:57

标签: python inheritance design-patterns delegation abc

我有以下类使用额外的DelegatorParent类实现“委托设计模式”:

class DelegatorParent():

    def __init__(self):
        self.a = 'whatever'    

class ConcreteDelegatee():

    def myMethod(self):
        return 'myMethod'


class Delegator(DelegatorParent):

    def __init__(self):
        self.delegatee = ConcreteDelegatee()
        DelegatorParent.__init__(self)

    def __getattr__(self, attrname):
        return getattr(self.delegatee, attrname)

a = Delegator()
result = a.myMethod()

一切都很好。

现在我想在DelegatorParent中添加一个抽象方法,以确保始终定义“myMethod”。

from abc import ABCMeta, abstractmethod

class DelegatorParent():
    __metaclass__ = ABCMeta

    @abstractmethod
    def myMethod(self):
        pass

    def __init__(self):
        self.a = 'whatever'


class ConcreteDelegatee():

    def myMethod(self):
        return 'myMethod'


class Delegator(DelegatorParent):

    def __init__(self):
        self.delegatee = ConcreteDelegatee()
        DelegatorParent.__init__(self)

    def __getattr__(self, attrname):
        return getattr(self.delegatee, attrname)

    # This method seems unnecessary, but if I erase it an exception is
    # raised because the abstract method's restriction is violated
    def myMethod(self): 
        return self.delegatee.myMethod()


a = Delegator()
result = a.myMethod()

你能帮我找一个“优雅”的方式从“委托人”中删除“myMethod”......直觉告诉我它有点多余(考虑到定义了自定义getattr方法)。

更重要的是,请注意,通过此实现,如果我忘记在ConcreteDelegatee中定义myMethod,程序将编译,但如果我调用Delegator.myMethod(),它可能会在运行时崩溃,这正是我想通过使用避免的DelegatorParent中的抽象方法。

显然,一个简单的解决方案是将@abstractmethod移动到Delegator类,但我想避免这样做,因为在我的程序中,DelegatorParent是一个非常重要的类(而Delegator只是一个辅助类)。

3 个答案:

答案 0 :(得分:3)

您可以决定自动实现委托给ConcreteDelegatee的抽象方法。

对于每个抽象方法,检查它是否存在于ConcreteDelegatee类中,并将此方法实现为此类方法的委托。

from abc import ABCMeta, abstractmethod

class DelegatorParent(object):
    __metaclass__ = ABCMeta

    def __init__(self):
        self.a = 'whatever'

    @abstractmethod
    def myMethod(self):
        pass


class Delegatee(object):
    pass


class ConcreteDelegatee(Delegatee):    
    def myMethod(self):
        return 'myMethod'

    def myMethod2(self):
        return 'myMethod2'


class Delegator(DelegatorParent):

    def __new__(cls, *args, **kwargs):
        implemented = set()
        for name in cls.__abstractmethods__:
            if hasattr(ConcreteDelegatee, name):
                def delegated(this, *a, **kw):
                    meth = getattr(this.delegatee, name)
                    return meth(*a, **kw)
                setattr(cls, name, delegated)
                implemented.add(name)
        cls.__abstractmethods__ = frozenset(cls.__abstractmethods__ - implemented)
        obj = super(Delegator, cls).__new__(cls, *args, **kwargs)
        obj.delegatee = ConcreteDelegatee()
        return obj

    def __getattr__(self, attrname):
        # Called only for attributes not defined by this class (or its bases).
        # Retrieve attribute from current behavior delegate class instance.
        return getattr(self.delegatee, attrname)

# All abstract methods are delegared to ConcreteDelegatee
a = Delegator() 

print(a.myMethod()) # correctly prints 'myMethod'

print(a.myMethod2()) #correctly prints 'myMethod2'

这解决了主要问题(阻止ConcreteDelegatee忘记定义myMethod)。如果您忘记实现它们,仍会检查其他抽象方法。

__new__方法负责委派,让您__init__释放它。

答案 1 :(得分:0)

由于您使用ABCMeta,因此必须定义抽象方法。可以从__abstractmethods__集中删除您的方法,但它是frozenset。无论如何,它涉及列出所有抽象方法。

因此,您可以使用简单的描述符,而不是使用__getattr__

例如:

class Delegated(object):
    def __init__(self, attrname=None):
        self.attrname = attrname

    def __get__(self, instance, owner):
        if instance is None:
            return self
        delegatee = instance.delegatee
        return getattr(delegatee, self.attrname)


class Delegator(DelegatorParent):
    def __init__(self):
        self.delegatee = ConcreteDelegatee()
        DelegatorParent.__init__(self)

    myMethod = Delegated('myMethod')

这里的一个优点是:开发人员拥有" myMethod"被委派。

如果您尝试:

a = Delegator()
result = a.myMethod()

有效!但如果您忘记在myMethod课程中实施Delegator,则会遇到经典错误:

Traceback (most recent call last):
  File "script.py", line 40, in <module>
    a = Delegator()
TypeError: Can't instantiate abstract class Delegator with abstract methods myMethod

修改

此实现可以概括如下:

class DelegatorParent():
    __metaclass__ = ABCMeta

    @abstractmethod
    def myMethod1(self):
        pass

    @abstractmethod
    def myMethod2(self):
        pass

    def __init__(self):
        self.a = 'whatever'


class ConcreteDelegatee1():
    def myMethod1(self):
        return 'myMethod1'


class ConcreteDelegatee2():
    def myMethod2(self):
        return 'myMethod2'


class DelegatedTo(object):
    def __init__(self, attrname):
        self.delegatee_name, self.attrname = attrname.split('.')

    def __get__(self, instance, owner):
        if instance is None:
            return self
        delegatee = getattr(instance, self.delegatee_name)
        return getattr(delegatee, self.attrname)


class Delegator(DelegatorParent):
    def __init__(self):
        self.delegatee1 = ConcreteDelegatee1()
        self.delegatee2 = ConcreteDelegatee2()
        DelegatorParent.__init__(self)

    myMethod1 = DelegatedTo('delegatee1.myMethod1')
    myMethod2 = DelegatedTo('delegatee2.myMethod2')


a = Delegator()
result = a.myMethod2()

在这里,我们可以指定委托者名称和委托方法。

答案 2 :(得分:0)

这是我目前的解决方案。它解决了主要问题(防止ConcreteDelegatee忘记定义myMethod),但我还是不相信,因为我仍然需要在Delegator中定义myMethod,这似乎是多余的

from abc import ABCMeta, abstractmethod

class DelegatorParent(object):
    __metaclass__ = ABCMeta

    def __init__(self):
        self.a = 'whatever'

    @abstractmethod
    def myMethod(self):
        pass


class Delegatee(object):
    def checkExistence(self, attrname):
        if not callable(getattr(self, attrname, None)):
            error_msg = "Can't instantiate " + str(self.__class__.__name__) + " without abstract method " + attrname
            raise NotImplementedError(error_msg)


class ConcreteDelegatee(Delegatee):    
    def myMethod(self):
        return 'myMethod'

    def myMethod2(self):
        return 'myMethod2'


class Delegator(DelegatorParent):
    def __init__(self):
        self.delegatee = ConcreteDelegatee()
        DelegatorParent.__init__(self)
        for method in DelegatorParent.__abstractmethods__:
            self.delegatee.checkExistence(method)

    def myMethod(self, *args, **kw):
        return self.delegatee.myMethod(*args, **kw)

    def __getattr__(self, attrname):
        # Called only for attributes not defined by this class (or its bases).
        # Retrieve attribute from current behavior delegate class instance.
        return getattr(self.delegatee, attrname)



# if I forget to implement myMethod inside ConcreteDelegatee, 
# the following line will correctly raise an exception saying 
# that 'myMethod' is missing inside 'ConcreteDelegatee'.
a = Delegator() 

print a.myMethod() # correctly prints 'myMethod'

print a.myMethod2() #correctly prints 'myMethod2'