C++ design question

Hi there,

This is a question about design in C++. Maybe you have some suggestions on what’s the best way to approach this.

I’m working on refactoring some C++ classes that are primarily created to speed up some Matlab functions.
Some of these classes have to be able to be instantiated in 3 ways:
-using parameters in the constructor (traditional way)
-from a file that contains all the necessary data.
-from a structure mxArray. This structure comes from the Matlab interface and it can contain heterogeneous data. There are functions to query mxArray for fields and get pointers to the data.

Other classes contain a lot of complex data that is created with external tools in Matlab. A constructor for these classes accepting all the internal data would require too many parameters (maybe I should refactor this??), so for these classes I only plan to instantiate them in 2 ways:
-from a file that contains all the necessary data.
-from a mxArray structure.

I would like to deal with all the classes in a consistent way. I don’t want these classes to have to know about mxArrays or files. That is, I don’t want constructors to take mxArrays as parameters. So ideally I would have some sort of interface factory that knows how the data is arranged in files and mxArrays so that it can return instantiated objects with the desired state. For the classes with few data members I would also like to be able to pass the data as constructors parameters.

Another desirable feature is that if the class in question is instantiated from a mxArray, I don’t want to copy the data, as this data can be quite big. What I want is to map the class data members to the various fields in the mxArray.


What’s the best way to do this?

I have thought of some possibilities, none of them perfect.
-One possibility would be to create these classes using parameters in their constructors or use setter functions to set the object in question to the desired state. This would be infeasible for the second type of classes which constructors would have lots of parameters or setters.

-Another option would be to allow that each class knows how to serialise itself, and be able to set an object to any desired state by reading a serialised version of the object. However, when I want to create the class from a mxArray I would have to serialise the data in the mxArray and I prefer to don’t have to copy data.

-Another option would be to make the class friend with another class that knows how to read files or knows how to access the relevant data in the mxArray. Then this class could just set the private data members of the class I want to initialise. I know that using friend classes is not very object oriented, I don’t know if there would be other problems with this option.

-Another option would be to make all the data members protected and the class that knows how to read files and mxArrays would inherit from it. Then the interface class could return a reference to the base class. For example, I want to instantiate a class A with my interface class B, which inherits from A, and B knows how to read files and mxArray. B would set all it’s data member (inherited from A) and then return a reference to the A part of B.

Any other options, patterns I could use? Or comments on the options I propose?

Thanks
Martin.
Topic archived. No new replies allowed.