I am dealing with a drawing app that allow the user to draw a set of Components (which are like the graph vertices) and they can be wired (like the graph edges) on each other to simulate how their connection propagate and affect each component based on their input, like a function on each node that takes all inputs, apply its function and set its outputs. Then, it propagates the value to other components connected to the outputs.
The struggle lies on the fact that my Component is also a Draw to be painted on a canvas. Usually problems like this are solved using the self drawing solution like the following example:
abstract class Component {
// component stuff
inputs: [] = [];
outputs: [] = [];
abstract function(): void;
// drawing stuff
x: number = 0;
y: number = 0;
draw(drawer: Drawer){
drawer.drawCircle(this.x, this.y, 20);
}
}
But it seems that mixing the draw of the with the meaning of that Vertice is bad design and really messy when trying to understand the code.
I have seen people suggesting creating different classes for Draw and Component and the draw holds the reference of the real component. It seems reasonable but I do not know if this is the more practical solution because it looks like you have to duplicate the whole structure in the form of graph just to separate the draw from it.
I would like to know if there is a better approach for this sort of problem.