Basically I do want the safefty of the Command's execute(), while I do not want the implementation of CompoundCommand's execute() for ExecutedCompoundCommand, but I do want to just rely on the add(), remove() and undo() operations of CompoundCommand.

As a student, working on a project with required javadoc and unit testing, it is really needed that there is as few code duplication as possible, as it only makes more work.

4 Answers
4

I think it is a design flaw. You can apply the Template Method Pattern [GOF 325]

Intent: Define the skeleton of an algorithm in an operation, deffering
some steps to subclasses. Template Method lets subclasses redefine
certain steps of an algorithm without changing the algorithm's
structure.

From Gang of Four Design Patterns

You want to be sure that certain steps are executed. So you would make a final template method execute() and delegate to the doExecute() method, which can add additional logic and needs to be implemented by the subclasses.

I think this answer might be the most appropiate one for this case. I've had it in class, but it hasn't come to mind. But this is exactly what I want to do as I want to change the behaviour of the part inside the for-loop. This seems justifyable, whereas simply copying the execute() code from the Command class into ExecutedCompoundCommand's execute() is not really justifyable.
–
skiwiMar 30 '13 at 16:17

Take a look here. Basically it explains why you should never need to do what you are looking to do.

As quoted from the link:

You shouldn't be able to bypass the parent class's behaviour. It makes sense to sometimes be able to bypass your own class's behaviour (particularly from within the same method) but not your parent's.

In the example shown in the link, the argument made is that the "middle" class, so to speak, is implementing some functionality or validity checks which will be bypassed by "skipping" a class in the hierarchy.

Please quote the relevant information so that your answer actually answers the question. Otherwise just post the link as a comment, and leave the answering to somebody else!
–
Lightness Races in OrbitMar 30 '13 at 16:02

I have read this answer before, I understand it aswell, however I can not find a passage that is directly important (and would answer) my question. As I do not think that my example 100% follows what the author of that answer had in mind.
–
skiwiMar 30 '13 at 16:06

What you are trying to do is not in itself wrong in this case, it's just not allowed. So your best bet in my opinion is to implement add(), remove() and undo() as an interface
–
Jurgen CamilleriMar 30 '13 at 16:12