I am creating a main object, MACHINE, which will contain a diversity of several other objects -- AXIS, AMPLIFIER, ENDEFFECTOR, etc. The number of contained objects will vary depending on the MACHINE model object that I am using.For example, a certain machine might have one AXIS and one ENDEFFECTOR; another 3 AXIS and 2 ENDEFFECTORS, etc.I was planning to create subclasses with each machine model, and initialize the respective number of contained objects depending on the model. My fear is that there is going to be an explosion of machine models, as well as quite a lot of repeated code and settings among subclasses.I get the feeling that there is a cleaner way to implement this, but I am not sure how to. Any ideas will be greatly appreciated.
Designing classes containing the same objects but different amount of them
c++;design patterns;object oriented design
I would keep this with a single machine class which holds a vector or list of items. To initialize it you can either hardcode the initializing process by using something like an addPart() method or using a init file for each machine type and add a method (or constructor) that can read such init files.So first version would look something like this: (assuming all machine parts are sub of a base class like Part)Machine* smallMachine = new Machine;smallMachine->addPart(new Axis);smallMachine->addPart(new Amplifier);Or you could have methods for each possible type of part:Machine* smallMachine = new Machine;smallMachine->addAxis();smallMachine->addAmplifier();The second would simply use a filename for the constructor:Machine* smallMachine = new Machine(/home/machines/small_machine.ini);edit:There are many ways to handle this inside the machine class and it mainly depends on what the parts actually can do and how much the Macine class knows about them.Assume the machine know near to nothing about the parts, only that they can do something and will later only start each part once in a loop. In this case you could simply write a pure virtual base class Part;class Part{     virtual void work() = 0;}All classes that derive from Part must overwrite the work() method and that way implement their functionality. Machine knows nothing else and can use a simple vector of parts.class Axis : private Part{    virtual void work(){        // implement actual functionality        // may call lots of private methods of Axis to do this    }}If this doesn't work for you, it is in fact a better solution to keep several containers for each type of part. This especially if you plan to have some specific interactions between parts.If you use the ini solution, this wouldn't change much compared to the method solution, since after all, the constructor would need to call similar functions when evaluating the ini files content.The virtual base class is the one that would need the least changes in case you add new parts. None at all in best case. But it would be limited to the method (or several methods) that are declared in Part.The solution with different containers and more knowledge by Machine would need to be updated whenever you add a new type of part or add new functionality to a part. But if you need Machine to know specific details of parts, you won't get around this anyway.edit: From your comment above about implementing a CNC control system I would think, that you need a more complex structure. Maybe you should have a look in some good book about design patterns. I think this would be worth your time. As it looks each part would implement quite different kinds of functionality and they may even depend on each others state.