samedi 8 décembre 2018

Design decisions: Passing context to API methods?

I am stuck at a problem where I want to perform different functionalities by calling the same method. One design which I came up was to pass multiple modes to the same method.

For example, my API has three methods : start, update and end which will be calling update method of 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)

Proposed Solution: I can directly introduce all the functions individually at other API, instead of calling them through update. Something like below:

 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)

Question: Is there way better way of handling the above requirement and is this a code smell?

Aucun commentaire:

Enregistrer un commentaire