Design question: holding class metadata for dynamic search

I have a couple of base / interface classes, each with multiple derived classes. I need to store metadata for each derived class, which has many overlaps, but different values.

I also have a Factory class for instantiating derived classes implemented as a singleton and has multiple macros. For example, you should:

REGISTER_COMPONENT("MyComponent", MyComponentClass);
// create an instance of each component
BaseComponent *base;
foreach(QString component, ComponentRegister::componentList())
{
  base = ComponentRegister::create(component);
  // do stuff with base
}

      

The question arises: how and where to store metadata from a solid design point of view.

I was able to store data in the ComponentRegister as a QMap structure. When someone registers a component, they can also register their metadata with

REGISTER_COMPONENT_METADATA("MyComponent", MyMap);

      

If QVariant :: isValid () is for a specific key, you know the metadata is set and available.

Another way is static class variables or maybe static QMap class.

I see the benefits and fall back to both. Most of the metadata is a "QResources path for this class" that is not closely related to the business logic of the class itself.

Another problem with a static variable class method comes into play with inheritance. You cannot force static class variables to be overridden like you can with pure virtual functions. So in case someone forgets ... it might not be clear where in the inheritance tree the values ​​appear. If you need to access metadata using a series of virtual "getters" then the MetaData setting applies to all base class implementations.

With the data set and viewed in the Register, if you need to make changes (like changing the root path for resources), you can do so at one point ... in class registration calls, perhaps a header or wrapped in an application utility function. With static data, you will have to edit every class declaration.

Open to suggestions and thank you!

+2


a source to share


1 answer


If the data related to an object is not specific to a single instance, as the path in your example, my projects usually include the class that manages my collection of objects. This is where I put the metadata.

Example:



class zoo { std::vector<animals> zoo_animals; size_t count; }

      

count - metadata about animals.

0


a source







All Articles