我在尝试实现易于使用的抽象工厂时遇到了问题。
能够以这种方式定义具体工厂:
class MyConcreteFactory( ... ):
@classmethod
def __load(cls, key):
obj = ... # Loading instructions here
return obj
能够以这种方式使用concretre工厂
obj = MyConcreteFactory[key]
我试图为工厂定义一个元类,它覆盖了括号运算符并封装了工厂模式:
class __FactoryMeta(type):
__ressources = {}
@classmethod
def __getitem__(cls, key):
if key not in cls.__ressources:
cls.__ressources[key] = cls.__load(key)
return cls.__ressources[key]
@classmethod
def __load(cls, key):
raise NotImplementedError
class ConcreteFactory(metaclass=__FactoryMeta):
@classmethod
def __load(cls, key):
return "toto"
a = ConcreteFactory["mykey"]
print(a)
这失败了,因为调用的__load方法是来自元类的方法,而不是来自具体类的方法。结果是:
Traceback (most recent call last):
File "C:\Users\walter\workspace\Game\src\core\factories.py", line 34, in <module>
a = ConcreteFactory["mykey"]
File "C:\Users\walter\workspace\Game\src\core\factories.py", line 19, in __getitem__
cls.__ressources[key] = cls.__load(key)
File "C:\Users\walter\workspace\Game\src\core\factories.py", line 24, in __load
raise NotImplementedError
NotImplementedError
我试图从元类中删除__load方法,但后来我得到了这个(可预测的)错误:
Traceback (most recent call last):
File "C:\Users\walter\workspace\Game\src\core\factories.py", line 30, in <module>
a = ConcreteFactory["mykey"]
File "C:\Users\walter\workspace\Game\src\core\factories.py", line 19, in __getitem__
cls.__ressources[key] = cls.__load(key)
AttributeError: type object '__FactoryMeta' has no attribute '_FactoryMeta__load'
有没有办法从元类中访问类方法? 我错了,应该以另一种方式做到这一点吗?然后呢?
class __FactoryMeta(type):
ressources = {}
def __getitem__(cls, key):
if key not in cls.ressources:
cls.ressources[key] = cls.load(key)
return cls.ressources[key]
def load(cls, key):
raise NotImplementedError
class ConcreteFactory(metaclass=__FactoryMeta):
@classmethod
def load(cls, key):
return "toto"
a = ConcreteFactory["mykey"]
print(a)
答案 0 :(得分:1)
您不应在元类中使用@classmethod
。元类的一个实例就是类本身:
def __getitem__(cls, key):
实际上是班级和:
@classmethod
def __getitem__(metacls, key):
将元类作为第一个参数。
Eiher案例,我认为元类会使这个问题变得更加复杂。我相信一种更可行的方法是创建一个基础工厂类,相应地对其进行子类化,并使用子类实例作为工厂。