lundi 2 janvier 2017

If I find myself defining a Component class and a ComponentInstance class, am I doing something wrong?

I am trying to write a program for designing electrical circuits in C++. At run-time, the user will be able to create a new type of component and will define its various attributes (e.g. number of pins, names of pins). They will then be able to create a circuit which includes multiple instances of these various user-created component types. When I tried to code this, I initially created a Component class, which would represent a type of component and store its number of pins, pin names, etc., as well as a ComponentInstance class to represent an instance of this component as part of a circuit. The ComponentIntance class maintains a reference back to its Component so that any changes to the Component will be reflected in its instances.
When I sat down and thought about my code, this seemed a bit clumsy. I am using an object-oriented language which provides facilities for exactly this kind of relationship: classes to represent a type and objects to represent an instance. The issue that I am having is that I don't know how to take advantage of these facilities because the type relies on user-provided input. Additionally, the user should be able to change the properties of the type and have these changes reflected in existing instances.
I thought that perhaps I should use some sort of template trickery to create this system, but I don't know if this is the right method, and even if it is, I don't know how to actually implement it. Any assistance would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire