问题描述
我正在使用构建器模式来分离一堆不同的配置可能性.基本上,我有一堆名为 ID 的类(类似于 ID12345).这些都继承自基础构建器类.在我的脚本中,每次运行此应用程序时,我都需要为每个类(大约 50 个)实例化一个实例.所以,我想看看是否不做这样的事情:
ProcessDirector = ProcessDirector() ID12345 = ID12345() ID01234 = ID01234() ProcessDirector.construct(ID12345) ProcessDirector.construct(ID01234) ID12345.run() ID01234.run()
我可以做这样的事情吗(我知道这行不通):
IDS = ["ID12345", "ID01234"] ProcessDirector = ProcessDirector() for id in IDS: builder = id() #some how instantiate class from string ProcessDirector.construct(builder) builder.run()
这样,当我以后需要添加一个新的时,我所要做的就是将 id 添加到 IDS 列表中,而不是在整个代码中添加新的 ID.
编辑
根据数据的来源,似乎有一些不同的意见.这些 ID 被输入到其他人无权访问的文件中.我不是从命令行读取字符串,我希望将来在添加新 ID 时能够做的改动很少.
推荐答案
不完全确定这是你想要的,但它似乎是一种更 Python 的方式来实例化字符串中列出的一堆类:
class idClasses: class ID12345:pass class ID01234:pass # could also be: import idClasses class ProcessDirector: def __init__(self): self.allClasses = [] def construct(self, builderName): targetClass = getattr(idClasses, builderName) instance = targetClass() self.allClasses.append(instance) IDS = ["ID12345", "ID01234"] director = ProcessDirector() for id in IDS: director.construct(id) print director.allClasses # [<__main__.ID12345 instance at 0x7d850>, <__main__.ID01234 instance at 0x7d918>]
问题描述
I'm using a builder pattern to seperate a bunch of different configuration possibilities. Basically, I have a bunch of classes that are named an ID (something like ID12345). These all inherit from the base builder class. In my script, I need to instantiate an instance for each class (about 50) every time this app runs. So, I'm trying to see if instead of doing something like this:
ProcessDirector = ProcessDirector() ID12345 = ID12345() ID01234 = ID01234() ProcessDirector.construct(ID12345) ProcessDirector.construct(ID01234) ID12345.run() ID01234.run()
Can I do something like this (I know this doesn't work):
IDS = ["ID12345", "ID01234"] ProcessDirector = ProcessDirector() for id in IDS: builder = id() #some how instantiate class from string ProcessDirector.construct(builder) builder.run()
That way, when I need to add a new one in the future, all I have to do is add the id to the IDS list, rather than peppering the new ID throughout the code.
EDIT
Looks like there are some different opinions based on where the data is coming from. These IDs are entered in a file that no one else has access to. I'm not reading the strings from the command line, and I'd like to be able to do as little alteration when adding a new ID in the future.
推荐答案
Not totally sure this is what you want, but it seems like a more Python'y way to instantiate a bunch of classes listed in a string:
class idClasses: class ID12345:pass class ID01234:pass # could also be: import idClasses class ProcessDirector: def __init__(self): self.allClasses = [] def construct(self, builderName): targetClass = getattr(idClasses, builderName) instance = targetClass() self.allClasses.append(instance) IDS = ["ID12345", "ID01234"] director = ProcessDirector() for id in IDS: director.construct(id) print director.allClasses # [<__main__.ID12345 instance at 0x7d850>, <__main__.ID01234 instance at 0x7d918>]