mercredi 24 juillet 2019

C++ class hierarchy in which to add functionality with an interface

C++. Imagine the following situation.

There's a class hierarchy of classes deriving from some base class A. We cannot modify A because it is outside of our scope. (Provided by a library, it is a MFC CView class, but that shouldn't matter here)

So there are A1, A2 etc which are different classes somehow derived from A and providing specific functionality.

Now imagine we define some new interface I to provide some new functionality.

Classes for concrete objects of the application will inherit from both one of the As and I. Let's call them Bs. (There are again several of them, like B1 derived from A1 and I, B2 derived from A2 and I etc.)

Now it happens that to implement the interface of I, there is a lot of common code that needs functionality from A. How can we organize the class hierarchy without repeating ourselves too much.

So for instance if there is a function I::f that needs to call A::f, for all derived classes Bn. It seems like waste to re-implement I::f for every Bn. But obviously, we cannot call A::f directly from I::f, as they aren't related.

I hope you get the point.

What is the pattern that can help us here?

Aucun commentaire:

Enregistrer un commentaire