Rod*_*Ney 2 python oop design-patterns
我必须以依赖顺序的方式顺序执行多个动作.
StepOne(arg1, arg2).execute()
StepTwo(arg1, arg2).execute()
StepThree(arg1, arg2).execute()
StepFour(arg1, arg2).execute()
StepFive(arg1, arg2).execute()
Run Code Online (Sandbox Code Playgroud)
它们都从同一个Step类继承并获得相同的2个args.
class Step:
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
def execute(self):
raise NotImplementedError('This is an "abstract" method!')
Run Code Online (Sandbox Code Playgroud)
按顺序执行这些操作的最惯用方法是什么?是否有适用于此处的设计模式?
您可以创建步骤类的列表,然后实例化并在循环中调用它们.
step_classes = [StepOne, StepTwo, StepThree, ...]
for c in step_classes:
c(arg1, arg2).execute()
Run Code Online (Sandbox Code Playgroud)