我需要super.super.method()->可能的设计缺陷? - java

我发现自己需要在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();
            }
        }
    }
}

基本上,我确实想要命令的execute()的安全性,而我不想为CompoundCommand实现ExecutedCompoundCommand的execute(),但我确实希望仅依赖于的add(),remove()和undo()操作CompoundCommand

作为一个学生,在使用必需的Javadoc和单元测试进行项目时,确实需要尽可能减少代码重复,因为这样做只会使工作更多。

参考方案

我认为这是设计缺陷。您可以应用模板方法模式[GOF 325]

目的:根据操作定义算法的框架
子类的一些步骤。模板方法可让子类重新定义
算法的某些步骤而无需更改算法的
结构体。

从四个设计模式的帮派

您要确保执行某些步骤。因此,您需要使最终的模板方法execute()并委托给doExecute()方法,该方法可以添加其他逻辑,并且需要由子类实现。

public final void execute() {
  importantOperation();
  runsAlways();
  doExecute();
}

public abstract void doExecute(); // Override in subclasses

与哪些运算符>>兼容 - java

我这里没有什么代码int b=3; b=b >> 1; System.out.println(b); 它可以完美工作,但是当我将变量b更改为byte,short,float,double时,它包含错误,但是对于变量int和long来说,它可以完美工作,为什么它不能与其他变量一起工作? 参考方案 位移位运算符(例如>>)与任何整数类型兼…

JAVA:如何检查对象数组中的所有对象是否都是子类的对象? - java

我有一个对象数组。现在,我要检查所有这些对象是否都是MyObject的实例。有没有比这更好的选择:boolean check = true; for (Object o : justAList){ if (!(o instanceof MyObject)){ check = false; break; } } java大神给出的解决方案 如果您不喜欢循环,则…

Java:在跳过中间继承的超类时调用基本超类方法 - java

假设我有三节课:class A { public void method() { /* Code specific to A */ } } class B extends A { @Override public void method() { /*Code specific to B*/ super.method(); } } class C extend…

Java 8流:处理空值 - java

以下代码为属性Salary为null抛出NPE。class Person具有以下属性:字符串:name,整数:age,整数:薪金salary在此处可以为null。我想创建一份工资清单。persons.stream().mapToDouble(Person::getSalary).boxed().collect(Collectors.toList())在这里,…

Java 8 Streams过滤和收集是否返回对列表中相同对象的引用? - java

例如考虑下面的代码List<Reference> references = context.getReferences() .stream() .filter(ref -> ref.getCondition() == SOMETHING_DESIRABLE) .collect(Collectors.toList()); 现在,如果我更改了引…