我发现自己需要在 java 中调用 super.super.method(),这是不可能的。
我只是想知道我的设计是否存在设计缺陷?
课程:
package solvers.command;
/**
*
* @author student
*/
public abstract class Command {
private boolean executed; //executed state
/**
* Constructs a new Command object.
*
* @modifies this.executed = false
*/
public Command() {
this.executed = false;
}
/**
* Executes this command.
*
* @modifies executed = true
* @pre {@code !executed}
* @throws IllegalStateException if {@code executed}
*/
public void execute() {
if (executed) {
throw new IllegalStateException("solvers.command.Command.execute: already executed");
}
executed = true;
}
/**
* Undoes this command.
*
* @modifies executed = false
* @pre {@code executed}
* @throws IllegalStateException if {@code !executed}
*/
public void undo() {
if (!executed) {
throw new IllegalStateException("solvers.command.Command.undo: not executed yet");
}
executed = false;
}
/**
* Returns the executed state
*
* @return executed state
*/
public boolean getExecuted() {
return executed;
}
}
package solvers.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author student
*/
public class CompoundCommand extends Command {
List<Command> commands; //list of commands
/**
* Creates a new CompoundCommand.
*
* @modifies this.commands is initialised
*/
public CompoundCommand() {
super();
this.commands = new ArrayList<>();
}
/**
* Adds a command to the list of commands.
*
* @param command The new command
* @pre {@code command != null}
* @throws IllegalArgumentException if {@code command == null}
*/
public void add(final Command command) {
if (command == null) {
throw new IllegalArgumentException("solvers.command.CompoundCommand.add: "
+ "command == null");
}
commands.add(command);
}
/**
* Removes a command from the list of commands.
*
* @param command The command to be removed
* @pre {@code command != null && commands.contains(command}
* @throws IllegalArgumentException if {@code command == null || !commands.contains(command)}
*/
public void remove(final Command command) {
if (command == null) {
throw new IllegalArgumentException("solvers.command.CompoundCommand.remove: "
+ "command == null");
}
if (!commands.contains(command)) {
throw new IllegalArgumentException("solvers.command.CompoundCommand.remove:"
+ "command is not found in commands");
}
commands.remove(command);
}
/**
* Returns if the list of commands is empty.
*
* @return {@code commands.isEmpty()}
*/
public boolean isEmpty() {
return commands.isEmpty();
}
@Override
public void execute() {
super.execute();
for (Command c : commands) {
c.execute();
}
}
@Override
public void undo() {
super.undo();
Collections.reverse(commands);
for (Command c : commands) {
c.undo();
}
Collections.reverse(commands);
}
}
package solvers.command;
/**
*
* @author student
*/
public class ExecutedCompoundCommand extends CompoundCommand {
/**
* Creates a new ExecutedCompoundCommand.
*/
public ExecutedCompoundCommand() {
super();
}
@Override
public void add(final Command command) {
if (!command.getExecuted()) {
throw new IllegalStateException("solvers.command.ExecutedCompoundCommand.add: "
+ "command has not been executed yet.");
}
super.add(command);
}
@Override
public void execute() {
super.super.execute(); /* Does not work obviously */
for (Command c : commands) {
if (!c.getExecuted()) {
c.execute();
}
}
}
}
基本上我确实想要 Command 的安全性execute()
,而我不希望执行CompoundCommand
'execute() for ExecutedCompoundCommand
,但我确实想只依赖CompoundCommand
.
作为一名学生,在一个需要 javadoc 和单元测试的项目上工作,确实需要尽可能少的代码重复,因为它只会做更多的工作。