dimanche 20 septembre 2020

Is there an alternative to using multiple inheritance in python in my case?

Let's say there are several classes

class Order:
  def create_basic_order(self):
    pass
    # something here

  def create_difficult_order(self):
    pass
    # something here  


class Point:
  def create_full_point(self):
    pass
    # something here

  def create_limited_point(self):
    pass
    # something here

And the client sends a request with a specific command in json format. For example {"command": "create_limited_point"}.

And the server should execute the appropriate command. In this case: Point().create_limited_point().

All commands are different. The most obvious way to do it without the 'if construct' is like this:

class App(Order, Point):
  pass
  # maybe something here

# Code to handle client request
command = 'create_limited_point'
a = App()
method_to_call = getattr(a, command)
method_to_call()

The class App collects all the methods that the client can use. Is this a good way to accomplish the task at hand?

Aucun commentaire:

Enregistrer un commentaire