Note that there are some explanatory texts on larger screens.

plurals
  1. PODiamond inheritance (C++)
    primarykey
    data
    text
    <p>I know that having diamond inheritance is considered bad practice. However, I have 2 cases in which I feel that diamond inheritance could fit very nicely. I want to ask, would you recommend me to use diamond inheritance in these cases, or is there another design that could be better.</p> <p><strong>Case 1:</strong> I want to create classes that represent different kinds of "Actions" in my system. The actions are classified by several parameters: </p> <ul> <li>The action can be "Read" or "Write".</li> <li>The action can be with delay or without delay (It is not just 1 parameter. It changes the behavior significantly).</li> <li>The action's "flow type" can be FlowA or FlowB.</li> </ul> <p>I intend to have the following design:</p> <pre><code>// abstract classes class Action { // methods relevant for all actions }; class ActionRead : public virtual Action { // methods related to reading }; class ActionWrite : public virtual Action { // methods related to writing }; class ActionWithDelay : public virtual Action { // methods related to delay definition and handling }; class ActionNoDelay : public virtual Action {/*...*/}; class ActionFlowA : public virtual Action {/*...*/}; class ActionFlowB : public virtual Action {/*...*/}; // concrete classes class ActionFlowAReadWithDelay : public ActionFlowA, public ActionRead, public ActionWithDelay { // implementation of the full flow of a read command with delay that does Flow A. }; class ActionFlowBReadWithDelay : public ActionFlowB, public ActionRead, public ActionWithDelay {/*...*/}; //... </code></pre> <p>Of course, I will obey that no 2 actions (inheriting from Action class) will implement the same method.</p> <p><strong>Case 2:</strong> I implement the composite design pattern for a "Command" in my system. A command can be read, written, deleted, etc. I also want to have a sequence of commands, which can also be read, written, deleted, etc. A sequence of commands can contain other sequences of commands. </p> <p>So I have the following design:</p> <pre><code>class CommandAbstraction { CommandAbstraction(){}; ~CommandAbstraction()=0; void Read()=0; void Write()=0; void Restore()=0; bool IsWritten() {/*implemented*/}; // and other implemented functions }; class OneCommand : public virtual CommandAbstraction { // implement Read, Write, Restore }; class CompositeCommand : public virtual CommandAbstraction { // implement Read, Write, Restore }; </code></pre> <p>In addition, I have a special kind of commands, "Modern" commands. Both one command and composite command can be modern. Being "Modern" adds a certain list of properties to one command and composite command (mostly same properties for both of them). I want to be able to hold a pointer to CommandAbstraction, and initialize it (via new) according to the needed type of command. So I want to do the following design (in addition to the above) :</p> <pre><code>class ModernCommand : public virtual CommandAbstraction { ~ModernCommand()=0; void SetModernPropertyA(){/*...*/} void ExecModernSomething(){/*...*/} void ModernSomethingElse()=0; }; class OneModernCommand : public OneCommand, public ModernCommand { void ModernSomethingElse() {/*...*/}; // ... few methods specific for OneModernCommand }; class CompositeModernCommand : public CompositeCommand, public ModernCommand { void ModernSomethingElse() {/*...*/}; // ... few methods specific for CompositeModernCommand }; </code></pre> <p>Again, I will make sure that no 2 classes inheriting from CommandAbstraction class will implement the same method.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload