我正在处理其他开发人员留下的一些代码。在这里,我们有几个项目可以合作。在运行时实例化项目对象。然后在其他类中使用。 Projects中没有方法,并且该类保留纯配置值。
class BaseProject(metaclass=ABCMeta):
"""
abstract class for projects
concrete version of this needed only to keep transition IDs in case of custom workflow.
"""
@property
@abstractmethod
def project_id(self):
pass
@property
@abstractmethod
def transition_id(self):
pass
@staticmethod
def by_name(name):
for prj in BaseProject.__subclasses__():
if name.lower() == prj.__name__.lower():
return prj()
return Project3()
@staticmethod
def by_card_name(name):
for prj in BaseProject.__subclasses__():
if name.split("-")[0].lower() == str(prj.__name__).lower():
return prj()
return Project3()
# concrete projects
class Project1(BaseProject):
project_id = 12334
transition_id = 444
class Project2(BaseProject):
project_id = 4451
transition_id = 88
class Project3(BaseProject):
project_id = 12346
transition_id = 88
在打开/关闭原理方面看起来不错。但是我有点困惑,值本身在类中而不在配置中。
顺便说一句,我们预计将来不会有更多项目,并且现有项目几乎不会改变。