2

So I have a Signal class and a client class which manages a list of Signal objects. The class provides a bunch of interfaces, one of them is an update()-function.

The update()-code is completely different for each signal. Thus, one would assume that's a job for derivation.

However, the client manages tens, maybe even a hundred different Signal objects. There is no way I want to have these many child classes.

I'm thinking of introducing a member updateFunction, which is a function pointer type. Now, the client could set this function (maybe via constructor) during the Signal's creation process. Then, the client can call update() for each Signal object and the update()-function would call the function pointer internally.

Would do you suggest? Is there a pattern for this?

  • 1
    IMO there is not much difference between having 100s classes with one function and 100 plain functions. – Euphoric Feb 10 '16 at 11:42
  • Are all of your 100 classes completely unique, or does this high number result from some combinatorial effect the combination of some orthogonal concepts (ex: the combinaton of persitent/non-presistant with audio/video/audio-video with hig-quality/low-quality, would lead to 12 different classes of signals) ? – Christophe Feb 15 '16 at 19:44
4

If you have multiple types of Signals that have to be updated in different ways, then the cleanest solution is to create a sub-class for each type of Signal, where the sub-classes only implement the different update behaviours.

The proposal for storing a function pointer in the Signal class is just emulating manually what the compiler does for you when you create a sub-class with the added downside that the maintenance programmers (including you in a few months time) will have a harder time understanding the code.

  • That seems reasonable. Function pointers often try to to achieve OOP in non-OOP languages, which is not needed here. However, we're talking of maybe 50-100 sub-classes. I somehow have a bad feeling with this. Maybe I can come up with another concept... – lugge86 Feb 11 '16 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.