There is a swarm
of objects. When a new unit
of certain kind appears on the frame, the swarm
integrates this object by calling some add_new_unit
method. Above the swarm
is a controller
abstraction, which handles multiple swarms of different types and the interaction with an image processing abstraction that detects units on the frame. Above the controller
is a GUI
layer.
I want to add a new widget to the GUI
every time a new unit
is added to the swarm
. Essentially, I need to call some method of GUI
abstraction object from the swarm
, but there is the controller
object in the hierarchy between them. Moreover, I want to be able to update this widget when some methods of unit
are called (i.e. move the position of the widget together with unit
's position)
Going top-to-bottom: GUI
knows only about controller
, controller
knows only about swarm
and swarm
knows only about its units
. I tried to decouple everything as much as possible and isolate every layer. Basically now I am trying to make a way to communicate the information from the bottom (from a unit
, i. e. its position, speed and other status parameters) all the way to the top, when some events on a unit
or swarm
levels happen. I want to keep controller
independent of GUI
as much as possible.
Is there a way to achieve the above-stated goal without coupling objects from the bottom to the top - i. e. without letting a unit
know about a swarm
, or without letting a controller
know about GUI
?
I am thinking about Observer that will report to controller
, which fires some handler method every time it observes add_new_unit
of the swarm
called. But in this case do I need to create Observer that will report to GUI
, that will observe `controller``s observer? Something tells me there should be a way to do that which does not involve so much redundancy.
The language in question is Python 3.7
Here is some code to demonstrate - I want GUI.add_new_widget_for_unit
to be called every time Swarm.add_new_unit
is called, and later modify that widget when some methods of Unit
are called:
class Unit:
def __init__(self, x, y):
self.x = x
self.y = y
class Swarm:
def __init__(self):
self.units = []
def update(self, units):
"""
Some updating/tracking logic here...
"""
if new_units_on_image:
new_unit = Unit(*params)
self.add_new_unit(new_unit)
def add_new_unit(self, unit)
self.units.append(unit)
class Controller:
def __init__(self):
self.swarm = Swarm()
self.image_processing = ImageProcessing()
def update(self):
units_on_image = self.image_processing.update()
self.swarm.update(units_on_image)
class GUI:
def __init__(self):
self.controller = Controller()
def update(self):
"""
Gets updated by some timer
"""
self.controller.update()
def add_new_widget_for_unit(self, parameters_of_unit):
"""
Adding widget with certain parameters from the unit
"""