Mock classes - How to solve this multiple-inheritance problem?

Hello,

I have drawn a class diagram to illustrate my question, see the following link:

https://imgur.com/a/6RM96Rd

Its a simple class hierarchy with some abstract interface classes. The grey classes show some mockups which I want to use for testing. As usual, the mockups derive from the interface classes. This is fine for 'MockAnimal' but confronts me with some problems at 'MockFish' and MockBird'.

To not copy code and to simplify my mockups, it would be great to use the mocked methods of 'MockAnimal' in these classes (multiple inheritance). Then I would just have to override/mock the methods from the sub-interfaces ('IFish' and 'IBird'). This wont work because im facing some kind of the diamond problem here i guess, because the methods of 'MockAnimal' are also implemented in 'ConcreteAnimal'. Thereofore my 'MockFish' and 'MockBird' will get those two implementations but I just want the ones from 'MockAnimal'.

As a workaround I changed my class hierarchy and just derived 'MockFish' and 'MockBird' from the interfaces 'IFish' and 'IBird'. But thats not a good solution in my opinion because the mockups will use the whole implementations of 'ConcreteAnimal'.

I hope that was understandable. Is there any good solution to achieve what I want?

Thanks for any help in advance.

Greetings
Look up mixin templates. That might give you what you want.

If you try this, Mock would be the mixin class that specializes IFish or IBird and simply overrides the breathe function.

I have not done this a lot, so there may be pitfalls that I am unaware of. I'm sure others on this forum will let us know if there are.
Thanks for your answer.

I have read something about Mixins now and I am not sure if this will solve my problem (or I do not understand it right so far).

First of all, the classes shown in the diagram i posted above are strongly simplified. So the main interface 'IAnimal' has much more members in reality and the corresponding mock class 'MockAnimal' has to deal with all of them. This works great at all.

The interface 'IAnimal' gets extended by sub-interfaces 'IFish' and 'IBird'. When I write mockups for these sub-interfaces the class hierarchy is giving me some trouble. The mockups have to derive from 'IFish' and 'IBird' and therefore they got all the implementations of 'ConcreteAnimal' which I do not want. Instead I want the mocked methods, implemented in 'MockAnimal'. Then I would only have to override the sub-interface methods in my new mockup class.

To get back to your answer:
As far as I understand it, a mixin class is usually a template class which is parameterised on its base class.

If you try this, Mock would be the mixin class that specializes IFish or IBird and simply overrides the breathe function.

I do not understand how this will get me any further now. For my understanding, I face the same problems as before. Could you please describe your answer in more detail?
I just got back to this thread.

As I think back to the limited experience I have with mixins, I remember now that they were used to add orthogonal functionality to an inheritance hierarchy. Using mixins allowed for a second flavor of each of the subclasses, and because the mixin class provided add-on functionality unrelated to the class inheritance, no diamond inheritance issues were avoided.

Because you are trying to override a virtual function in your Mock class, this is a different problem that what I was trying to solve. Even if you are able to use mixins (you may be able to, I don't know and don't have time to investigate it), you will still have to figure out how to deal with the diamond inheritance.

Sorry for the poor advice.
Topic archived. No new replies allowed.