Python设计问题 – 带有不同签名的具体类

huangapple go评论66阅读模式
英文:

Python Design Question - Concrete classes with different signature

问题

我有两个类,它们具有相似的方法;然而,在一个类中有一些不同的强制参数。

例如。

class NamespaceScoped:
    def get_object(self, name, namespace):
        pass
    def delete_object(self, name, namespace):
        pass

class ClusterScoped:
    def get_object(self, name):
        pass
    def delete_object(self, name):
        pass

我需要为上述类创建一个接口。由于这些方法大致相同,只是签名略有不同,创建一个具有相同操作但允许实现类具有不同签名的抽象类是否是一个好的设计?

例如。

class InterfaceClient(ABC):
    def get_object(self, name):
        pass
    def delete_object(self, name):
        pass

在Pycharm中,我会收到关于class NamespaceScoped签名不同的警告。

我可以在技术上使用**kwargs,但我认为我不能轻松地强制执行强制参数。

采用上述方法是否是一个好的设计?另一种方法是有两个抽象类(一个带有强制命名空间,另一个没有);然而,我认为这两种方法中方法的声明有点重复。

英文:

I have two classes with similar methods; however, there are some different mandatory parameters in one class.

e.g.

class NamespaceScoped:
    def get_object(self, name, namespace):
        pass
    def delete_object(self, name, namespace):
        pass

class ClusterScoped:
    def get_object(self, name):
        pass
    def delete_object(self, name):
        pass

I need to create an interface for the above classes. Since the methods are more or less the same, with only the signature varying a bit - is it a good design to have an Abstract class with the same operations but allow the implementation class to have a different signature?

e.g.

class InterfaceClient(ABC):
    def get_object(self, name):
        pass
    def delete_object(self, name):
        pass

In Pycharm - I get the warning that the signature is different for class NamespaceScoped.

I could technically do **kwargs, but I don't think I can enforce the mandatory parameters easily.

Is it an ok design to go with the above approach? An alternative to this is to have two abstract classes (one with namespace mandatory and the other with not); however, I see the declaration of the methods as are kind of duplicate.

答案1

得分: 2

如果你想要有一个共同的接口,我完全同意@deceze在评论中的说法:

如果函数的签名不同,你无法合理地定义一个共同的接口。

所以你应该使它们的签名相似。

从另一个角度来看,InterfaceClient 的两个方法都是实例方法,NamespaceScoped 类可以拥有一个 namespace 成员属性(由构造函数或一个设置器方法提供),从而使得能够为 NamespaceScopedClusterScoped 类提供一个共同的接口。

英文:

If you want to have one common interface, then I totally agree with the @deceze's comment above:
> You can't reasonably define a common interface if the signatures of the functions are different.

So you should make their signatures similar to each other.

From another point of view, both methods of the InterfaceClient are instance methods, the NamespaceScoped class can have a namespace member property (provided by the constructor or a setter method) by giving the ability to have one common interface for both NamespaceScoped and ClusterScoped classes.

from abc import ABC, abstractmethod


class InterfaceClient(ABC):
    @abstractmethod
    def get_object(self, name):
        pass

    @abstractmethod
    def delete_object(self, name):
        pass


class NamespaceScoped(InterfaceClient):
    def __init__(self, namespace):
        self.namespace = namespace

    def get_object(self, name):
        pass

    def delete_object(self, name):
        pass


class ClusterScoped(InterfaceClient):
    def get_object(self, name):
        pass

    def delete_object(self, name):
        pass

huangapple
  • 本文由 发表于 2023年2月8日 10:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定