重复代码命令 Class Java
Duplicate code Command Class Java
我有两个 类 实现命令(使用命令模式),一个是创建 sheet:
public class CommandCreateSheet implements Command {
private Workbook workbook;
private String sheetName;
public CommandCreateSheet(Workbook workbook, String workSheetName) {
this.workbook = workbook;
this.sheetName = workSheetName;
}
@Override
public void execute() {
this.workbook.addSheet(this.sheetName);
}
@Override
public void undo() {
this.workbook.removeSheet(this.sheetName);
}
}
另一个是删除:
public class CommandDeleteSheet implements Command {
private Workbook workbook;
private String sheetName;
public CommandDeleteSheet(Workbook workbook, String workSheetName) {
this.workbook = workbook;
this.sheetName = workSheetName;
}
@Override
public void execute() {
this.workbook.removeSheet(this.sheetName);
}
@Override
public void undo() {
this.workbook.addSheet(this.sheetName);
}
}
如何避免重复代码?
谢谢!!
您可以创建两个命令都实现的中间摘要 class。
public abstract class AbstactSheetCommand implements Command
{
private Workbook workbook;
private String sheetName;
protected AbstractSheetCommand(Workbook workbook, String workSheetName)
{
this.workbook = workbook;
this.sheetName = workSheetName;
}
public abstract void execute();
public abstract void undo();
}
然后你的子class会小很多:
public class CommandCreateSheet extends AbstractSheetCommand
{
public CommandCreateSheet(Workbook workbook, String workSheetName)
{
super(workbook, workSheetName);
}
@Override
public void execute() { this.workbook.addSheet(this.sheetName); }
@Override
public void undo() { this.workbook.removeSheet(this.sheetName);}
}
public class CommandDeleteSheet extends AbstractSheetCommand
{
public CommandDeleteSheet(Workbook workbook, String workSheetName)
{
super(workbook, workSheetName);
}
@Override
public void execute() { this.workbook.removeSheet(this.sheetName); }
@Override
public void undo() { this.workbook.addSheet(this.sheetName); }
}
这样做的好处是,您可以轻松地添加具有更具体功能的相同 "category"(可以这么说)的新命令。
public class CommandRenameSheet extends AbstractSheetCommand
{
private String newWorkSheetName;
public CommandDeleteSheet(Workbook workbook, String oldWorkSheetName, String newWorkSheetName)
{
super(workbook, oldWorkSheetName);
this.newWorkSheetName = newWorkSheetName;
}
@Override
public void execute()
{ this.workbook.renameSheet(this.sheetName, newWorkSheetName); }
@Override
public void undo()
{ this.workbook.renameSheet(newWorkSheet, this.sheetName); }
}
对于像你这样短的代码,你通过尝试消除代码来节省很少,而且你节省的代码越多,你的解决方案就越复杂。
但是这里有一个可能的 Java 8 实现使用抽象 class。
public abstract class SheetCommand implements Command {
private Runnable executeAction, undoAction;
public SheetCommand(Runnable executeAction, Runnable undoAction) {
this.executeAction = executeAction;
this.undoAction = undoAction;
}
@Override
public void execute() {
executeAction.run();
}
@Override
public void undo() {
undoAction.run();
}
}
public class CommandCreateSheet extends SheetCommand {
public CommandCreateSheet(Workbook workbook, String workSheetName) {
super(() -> workbook.addSheet(workSheetName),
() -> workbook.removeSheet(workSheetName));
}
}
public class CommandDeleteSheet extends SheetCommand {
public CommandDeleteSheet(Workbook workbook, String workSheetName) {
super(() -> workbook.removeSheet(workSheetName),
() -> workbook.addSheet(workSheetName));
}
}
此实现使用 lambda 表达式来表达您需要执行的操作。因为 lambda 表达式是闭包,所以您不需要显式保留实际的工作簿和 sheet 名称引用 - 它们隐藏在 lambda 表达式中。
对于 execute
和 undo
中更复杂的代码,您可能会像最初那样自己编写方法来做得更好 - 如果您尝试使用 lambda 来完成,您将结束编写比您最初拥有的代码更复杂、更难读的代码。
我有两个 类 实现命令(使用命令模式),一个是创建 sheet:
public class CommandCreateSheet implements Command {
private Workbook workbook;
private String sheetName;
public CommandCreateSheet(Workbook workbook, String workSheetName) {
this.workbook = workbook;
this.sheetName = workSheetName;
}
@Override
public void execute() {
this.workbook.addSheet(this.sheetName);
}
@Override
public void undo() {
this.workbook.removeSheet(this.sheetName);
}
}
另一个是删除:
public class CommandDeleteSheet implements Command {
private Workbook workbook;
private String sheetName;
public CommandDeleteSheet(Workbook workbook, String workSheetName) {
this.workbook = workbook;
this.sheetName = workSheetName;
}
@Override
public void execute() {
this.workbook.removeSheet(this.sheetName);
}
@Override
public void undo() {
this.workbook.addSheet(this.sheetName);
}
}
如何避免重复代码?
谢谢!!
您可以创建两个命令都实现的中间摘要 class。
public abstract class AbstactSheetCommand implements Command
{
private Workbook workbook;
private String sheetName;
protected AbstractSheetCommand(Workbook workbook, String workSheetName)
{
this.workbook = workbook;
this.sheetName = workSheetName;
}
public abstract void execute();
public abstract void undo();
}
然后你的子class会小很多:
public class CommandCreateSheet extends AbstractSheetCommand
{
public CommandCreateSheet(Workbook workbook, String workSheetName)
{
super(workbook, workSheetName);
}
@Override
public void execute() { this.workbook.addSheet(this.sheetName); }
@Override
public void undo() { this.workbook.removeSheet(this.sheetName);}
}
public class CommandDeleteSheet extends AbstractSheetCommand
{
public CommandDeleteSheet(Workbook workbook, String workSheetName)
{
super(workbook, workSheetName);
}
@Override
public void execute() { this.workbook.removeSheet(this.sheetName); }
@Override
public void undo() { this.workbook.addSheet(this.sheetName); }
}
这样做的好处是,您可以轻松地添加具有更具体功能的相同 "category"(可以这么说)的新命令。
public class CommandRenameSheet extends AbstractSheetCommand
{
private String newWorkSheetName;
public CommandDeleteSheet(Workbook workbook, String oldWorkSheetName, String newWorkSheetName)
{
super(workbook, oldWorkSheetName);
this.newWorkSheetName = newWorkSheetName;
}
@Override
public void execute()
{ this.workbook.renameSheet(this.sheetName, newWorkSheetName); }
@Override
public void undo()
{ this.workbook.renameSheet(newWorkSheet, this.sheetName); }
}
对于像你这样短的代码,你通过尝试消除代码来节省很少,而且你节省的代码越多,你的解决方案就越复杂。
但是这里有一个可能的 Java 8 实现使用抽象 class。
public abstract class SheetCommand implements Command {
private Runnable executeAction, undoAction;
public SheetCommand(Runnable executeAction, Runnable undoAction) {
this.executeAction = executeAction;
this.undoAction = undoAction;
}
@Override
public void execute() {
executeAction.run();
}
@Override
public void undo() {
undoAction.run();
}
}
public class CommandCreateSheet extends SheetCommand {
public CommandCreateSheet(Workbook workbook, String workSheetName) {
super(() -> workbook.addSheet(workSheetName),
() -> workbook.removeSheet(workSheetName));
}
}
public class CommandDeleteSheet extends SheetCommand {
public CommandDeleteSheet(Workbook workbook, String workSheetName) {
super(() -> workbook.removeSheet(workSheetName),
() -> workbook.addSheet(workSheetName));
}
}
此实现使用 lambda 表达式来表达您需要执行的操作。因为 lambda 表达式是闭包,所以您不需要显式保留实际的工作簿和 sheet 名称引用 - 它们隐藏在 lambda 表达式中。
对于 execute
和 undo
中更复杂的代码,您可能会像最初那样自己编写方法来做得更好 - 如果您尝试使用 lambda 来完成,您将结束编写比您最初拥有的代码更复杂、更难读的代码。