java – 我需要super.super.method() – >可能的设计缺陷?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 我需要super.super.method() – >可能的设计缺陷?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4071字,纯文字阅读大概需要6分钟。
内容图文
![java – 我需要super.super.method() – >可能的设计缺陷?](/upload/InfoBanner/zyjiaocheng/793/85ff26979ba84212939da67356691790.jpg)
我发现自己需要在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()的安全性,而我不想为ExecutedCompoundCommand实现CompoundCommand的execute(),但我确实只想依赖于add(),remove()和undo()操作CompoundCommand.
作为一名学生,在一个需要javadoc和单元测试的项目上工作,确实需要尽可能少的代码重复,因为它只能做更多的工作.
解决方法:
我认为这是一个设计缺陷.您可以应用模板方法模式[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.
来自四个设计模式的帮派
您希望确保执行某些步骤.因此,您将创建最终模板方法execute()并委托给doExecute()方法,该方法可以添加其他逻辑,并且需要由子类实现.
public final void execute() {
importantOperation();
runsAlways();
doExecute();
}
public abstract void doExecute(); // Override in subclasses
内容总结
以上是互联网集市为您收集整理的java – 我需要super.super.method() – >可能的设计缺陷?全部内容,希望文章能够帮你解决java – 我需要super.super.method() – >可能的设计缺陷?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。