Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Let's assume we have an XmlDoc class that contains basic functionality for dealing with an XML data structure and saving/loading data to/from a file. Now we have several subclasses, A, B and C. They all inherit from XmlDoc and add component-specific methods for setting and getting lots of data. They are like "interfaces" but also add an implementation for the signatures.

Finally, we have an ABCDoc class that joins all the "interfaces" via virtual multiple inheritence and adds some ABCDoc-specific stuff, such as using XMLDoc-methods to set an appropriate doc type. We may also have an ADoc class for only saving A data.

How is this pattern called? "Interface" is not really the right word since interfaces usually do not contain an implementation.

Bonus points for C++ code conventions.

share|improve this question

closed as off-topic by gnat, Bart van Ingen Schenau, Sebastian Negraszus, MichaelT, GlenH7 Mar 7 at 14:40

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
class hierarchy. (that kind of random design never had anything better names available) –  tp1 Nov 15 '11 at 18:51
2  
This question appears to be off-topic because it is a "name that thing" question. "Name that thing" are bad questions for the same reasons that "identify this obscure TV show, film or book by its characters or story" are bad questions: you can't Google them, they aren't practical in any way, they don't help anyone else, and allowing them opens the door for the asking of other types of marginal questions. See blog.stackoverflow.com/2012/02/lets-play-the-guessing-game –  Bart van Ingen Schenau Mar 7 at 8:46
    
@BartvanIngenSchenau OK, but I cannot delete the question, so I voted for close myself. –  Sebastian Negraszus Mar 7 at 9:55

2 Answers 2

It's called the "Diamond Problem" or, if it's not a problem for you, you can drop the "Problem" part. The issue is, how do you resolve which inherited method gets called from the derived class? You can override the function calls, but then you are only using that inheritance as an interface. If you are doing that, why not make abstract classes that don't inherit from the "master" class, then compose your new classes using multiple inheritance?

share|improve this answer
1  
Using virtual inheritence solves the diamond problem –  Sebastian Negraszus Nov 15 '11 at 23:02

Multiple Inheritance.

Maybe what you are hightlighting its the fact that all the base classes for ABCDoc, are also subclasses of the same base class, and not different class hierarchies.

Actually, I think its a mixture of 2 techniques, first, extending a base class for several cases:

.................................................................................................................
.................................................................................................................
....+-----------------------------------------+................+----------------------------------------------+..
....|                <<class>>                |....../|........|                   <<class>>                  |..
....|             GenericDocument             |...../.|...+----|                  TextDocument                |..
....+-----------------------------------------+----<..|---+....+----------------------------------------------+..
....| [+]  Load(...)         <<virtual>>      |.....\.|...|....| [+]  Load(...)                 <<override>>  |..
....| [+]  Save(...)         <<virtual>>      |......\|...|....| [+]  Save(...)                 <<override>>  |..
....| [+]  DoSomething(...)  <<non virtual>>  |...........|....| [+]  SpecificMethod_A(...)  <<non virtual>>  |..
....+-----------------------------------------+...........|....+----------------------------------------------+..
..........................................................|......................................................
..........................................................|....+----------------------------------------------+..
..........................................................|....|                  <<class>>                   |..
..........................................................|----|                BitmapDocument                |..
..........................................................|....+----------------------------------------------+..
..........................................................|....| [+]  Load(...)                 <<override>>  |..
..........................................................|....| [+]  Save(...)                 <<override>>  |..
..........................................................|....| [+]  SpecificMethod_B(...)  <<non virtual>>  |..
..........................................................|....+----------------------------------------------+..
..........................................................|......................................................
..........................................................|....+----------------------------------------------+..
..........................................................+----|                   <<class>>                  |..
...............................................................|                  XMLDocument                 |..
...............................................................+----------------------------------------------+..
...............................................................| [+]  Load(...)                 <<override>>  |..
...............................................................| [+]  Save(...)                 <<override>>  |..
...............................................................| [+]  SpecificMethod_C(...)  <<non virtual>>  |..
...............................................................+----------------------------------------------+..
.................................................................................................................
.................................................................................................................

Second, having a single class that encapsulates all the existing scenarios.

..........................................................
..........................................................
....+----------------------------------------------+......
....|                   <<class>>                  +---+..
....|                  TextDocument                |...|..
....+----------------------------------------------+...|..
....| [+]  Load(...)                 <<override>>  |...|..
....| [+]  Save(...)                 <<override>>  |...|..
....| [+]  SpecificMethod_A(...)  <<non virtual>>  |...|..
....+----------------------------------------------+...|..
.......................................................|...................................................................
....+----------------------------------------------+...|.../|.....+-----------------------------------------------------+..
....|                  <<class>>                   |...|../.|.....|                       <<class>>                     |..
....|                BitmapDocument                |---+-<..|-----|                 MultipleFormatDocument              |..
....+----------------------------------------------+...|..\.|.....+-----------------------------------------------------+..
....| [+]  Load(...)                 <<override>>  |...|...\|.....| [+]  Load(...)                        <<override>>  |..
....| [+]  Save(...)                 <<override>>  |...|..........| [+]  Save(...)                        <<override>>  |..
....| [+]  SpecificMethod_B(...)  <<non virtual>>  |...|..........| [+]  SelectDefaultFileFormat(...)  <<non virtual>>  |..
....+----------------------------------------------+...|..........+-----------------------------------------------------+..
.......................................................|...................................................................
....+----------------------------------------------+...|..
....|                  <<class>>                   +---+..
....|                 XMLDocument                  |..
....+----------------------------------------------+..
....| [+]  Load(...)                 <<override>>  |..
....| [+]  Save(...)                 <<override>>  |..
....| [+]  SpecificMethod_C(...)  <<non virtual>>  |..
....+----------------------------------------------+..
......................................................

I have a similar case. A class is composed of several base classes, that must descend from the same class, also.

I think there are 2 situations of these. If the intermediate base classes, can be consider to add more classes ("more file formats"), maybe the final class should be changed to this:

........................................................................................................................
....+-----------------------------------------+......./|......+-----------------------------------------------------+...
....|                <<class>>                |....../.|......|                       <<class>>                     |...
....|             GenericDocument             |-----<..|------|                 MultipleFormatDocument              |...
....+-----------------------------------------+......\.|......+-----------------------------------------------------+...
....| [+]  Load(...)         <<virtual>>      |.......\|......| [+]  Load(...)                        <<override>>  |...
....| [+]  Save(...)         <<virtual>>      |...............| [+]  Save(...)                        <<override>>  |...
....| [+]  DoSomething(...)  <<non virtual>>  |...............| [+]  SelectDefaultFileFormat(...)  <<non virtual>>  |...
....+------------------+----------------------+...............+-----------------------------------------------------+...
.......................|.................................................................^..............................
.......................|................................................................/.\.............................
.......................^.............................................................../...\............................
....................../.\..............................................................\.../............................
...................../...\..............................................................\./.............................
..................../-----\..............................................................V..............................
.......................+.................................................................|..............................
.......................|.................................................................|..............................
.......................|.......................................+-------------------------+---------------------------+..
.......................+---------------------------------------|                       <<class>>                     |..
...............................................................|                   <<Many classes>>                  |..
...............................................................+-----------------------------------------------------+..
...............................................................| [+]  Load(...)                        <<override>>  |..
...............................................................| [+]  Save(...)                        <<override>>  |..
...............................................................+-----------------------------------------------------+..
........................................................................................................................

Where it can support several file format classes like "add-ons".

There is a second situation.

In my app. the final class descend from 4 specific classes, I had to have access to the specific methods of those intermediate classes, and to add more classes didn't apply. And the 4 intermediate classes, had to be descendant of the same base or root class.

If it was another language, that supported multiple inheritance interfaces, maybe all these solutions could be more simple.

But, for the second case I "smell", an unnamed, Design Pattern.

share|improve this answer

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