Reputation: 2322
I'm trying to find a suitable inheritance configuration for the following problem:
My base class is an Agent Class which has 3 abstract methods:
class Agent():
__metaclass__ = abc.ABCMeta
def __init__(self):
@abc.abstractmethod
def bidding_curve(self):
@abc.abstractmethod
def evaluate_bidding_curve(self):
@abc.abstractmethod
def action(self):
An agent can either be simulated using an executable (dymo) or using an FMU (a zip file of C-source code). So I created 2 different types of Agents that inherit from the base Agent and implement their way of communicating with the simulation. Here is the implementation of the dymo simulation environment but the same goes for the FMU.
class DymoAgent(Agent):
def __init__(self, power, dymo):
"""
Agent based on the dymosim simulation environment.
@param dymo: The dymo that needs to be run
"""
super(DymoAgent, self).__init__()
self.dymo = dymo
def bidding_curve(self):
pass
def evaluate_bidding_curve(self, priority):
pass
def action(self, priority):
self.dymo.action()
The DymoAgent and the FMUAgent determine how my Agent will interact with the simulation environment.
Next I need an implementation of the Agent to determine how the Agent will interact with an application. However, I want this interaction to be independent of the simulation environment. E.g. I want to create an agent that can communicate with a Heater application and implements the logic to do so.
class Heater(WhatAgentShouldIUseHere):
pass
I want this Heater class to implement the abstract methods from the Agent base-class and the implementation of a Dymo- or FMUAgent so it knows how to interact with the simulation. But I don't want to write 2 Heater classes with the same logic (and thus basically the same code) inheriting from the different simulation Agents. I summarized the question in this image:
Is there a way to use inheritance to prevent this?
/Arnout
Upvotes: 3
Views: 418
Reputation: 59674
Make Heater
a simple object which accepts an agent in its constructor:
class Heater(object):
def __init__(self, agent):
self.agent = agent
# define other methods for interaction with Heater application
# in which you can access agent methods like self.agent.a_method(...)
Upvotes: 7