vendredi 8 novembre 2019

How best to let a user redefine a class method?

I have a library with a completely general implementation with the exception of a single class A. This class only has a single function int format(int i){} but can't be used as is: format depends on each different application and needs to be user-defined. In particular, format has quite a few if-then statements that need to be set correctly for the environment of the application. However the rest of the library is completely general, and will work with any choice of format that is within certain specifications.

The Question: What's the best way to let the user of my library define how format works? This needs to be done on the code level (as opposed to just using a configuration file).

The obvious idea would be to have the user directly rewrite the definition of format directly on the source code, but that doesn't sound like the most elegant way to do this.

Another idea would be to use inheritance: Have the user define a userA class inheriting from A and then hide the format function. But the rest of the library uses class A instead of userA. At this point, it could be possible to replace all instances of A with a typename desiredA and then have the user define that typename to be userA. This is not very elegant either.

There must be a better way to do this right?

Aucun commentaire:

Enregistrer un commentaire