设计决策:将上下文传递给API方法?

时间:2018-12-08 17:05:18

标签: python design-patterns

我陷入了一个问题,我想通过调用相同的方法来执行不同的功能。我提出的一种设计是将多种模式传递给同一方法。

例如,我的API有三种方法:开始,更新和结束,这将调用otherAPI的更新方法

class MyAPI(object):
    def start(self):
        otherAPI.update(self.data, 'start')

    def update(self):
        otherAPI.update(self.data, 'update')

    def stop(self):
        otherAPI.update(self.data, 'stop')


class otherAPI(object):
    def update(self,data, mode):
        if mode == 'start':
            self.func1(data)

        self.func2(data)

        if mode == 'stop'
            self.func3(data)

建议的解决方案: 我可以直接在其他API上单独介绍所有功能,而不是通过update调用它们。如下所示:

 class MyAPI(object):
    def start(self):
        otherAPI.func1(self.data)
        otherAPI.func2(self.data)

    def update(self):
        otherAPI.func2(self.data)

    def stop(self):
        otherAPI.func2(self.data)
        otherAPI.func3(self.data)

问题: 有没有更好的方法来处理上述要求,这是代码的味道吗?

0 个答案:

没有答案