这段代码是某种命令模式吗?
Is this code some kind of command pattern?
我有一个方法需要执行多个任务才能完成更大的任务。每个任务可能大约有 20-30 行代码,所以我决定每个任务有一个 class。
public void bigTask() {
TaskProcessor executor = new TaskProcessor();
executor.addTask(new Task1(some arguments here));
executor.addTask(new Task2(some other arguments here));
executor.addTask(new Task2(some other arguments here));
executor.run();
}
public interface Task {
public void execute();
}
public class Task1 implements Task {
@Override
public void execute() {
//Some code here
}
}
public class Task2 implements Task {
@Override
public void execute() {
//Some other code here
}
}
public class Task3 implements Task {
@Override
public void execute() {
//Some other code here
}
}
public class TaskProcessor implements Serializable {
private List<Task> tasksList;
public TaskProcessor () {
this.tasksList = new ArrayList<Task>();
}
public void addTask(Task task) {
this.tasksList.add(task);
}
public void execute() {
for (Task task : this.tasksList) {
task.execute();
}
}
}
对我来说,这段代码就像一个命令模式,但我不确定,因为每个任务的参数都是不同类型的,这与传统的命令模式不同。
您认为这可以被视为命令模式实现吗?
拆分一个大方法,你觉得这种做法可以吗?
谢谢
Do you think this could be considered a command pattern implementation?
我觉得"command pattern"够了。
Do you think this approach is OK for splitting a big method?
我们使用非常相似的方法来剖析长 "sequences" 小 "Actions"。但是我们添加了不同种类的"containers"。如:有时我有一系列的动作应该继续执行,即使一个条目失败。在其他情况下,整个序列应立即停止。另一种风格是每个 Action 也有一个 undo()
方法的序列,这样当某些 Action 失败时,序列容器可以回滚所有先前(通过的)Action。
根据您的情况,您可能 "good to go",但我认为您至少应该考虑 what/if 您的个人任务可能 失败 ,以及如何您的 TaskProcessor 容器应该对失败的步骤做出反应。
在结构方面,这段代码是命令设计模式的应用。四人帮书中模式参与者的映射如下:
Task
是模式中的Command
接口,有它的execute
方法;
Task1-3
为具体命令;
TaskProcessor
就是Invoker
,也就是"asks the command to carry out the request"
但是,就意图而言,有点不符。四人帮书中所说的命令模式的初衷是
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
但是,问题 "Do you think this approach is OK for splitting a big method?" 表明目标是提供复杂计算的模块化分解,这是不一样的。
我有一个方法需要执行多个任务才能完成更大的任务。每个任务可能大约有 20-30 行代码,所以我决定每个任务有一个 class。
public void bigTask() {
TaskProcessor executor = new TaskProcessor();
executor.addTask(new Task1(some arguments here));
executor.addTask(new Task2(some other arguments here));
executor.addTask(new Task2(some other arguments here));
executor.run();
}
public interface Task {
public void execute();
}
public class Task1 implements Task {
@Override
public void execute() {
//Some code here
}
}
public class Task2 implements Task {
@Override
public void execute() {
//Some other code here
}
}
public class Task3 implements Task {
@Override
public void execute() {
//Some other code here
}
}
public class TaskProcessor implements Serializable {
private List<Task> tasksList;
public TaskProcessor () {
this.tasksList = new ArrayList<Task>();
}
public void addTask(Task task) {
this.tasksList.add(task);
}
public void execute() {
for (Task task : this.tasksList) {
task.execute();
}
}
}
对我来说,这段代码就像一个命令模式,但我不确定,因为每个任务的参数都是不同类型的,这与传统的命令模式不同。
您认为这可以被视为命令模式实现吗? 拆分一个大方法,你觉得这种做法可以吗?
谢谢
Do you think this could be considered a command pattern implementation?
我觉得"command pattern"够了。
Do you think this approach is OK for splitting a big method?
我们使用非常相似的方法来剖析长 "sequences" 小 "Actions"。但是我们添加了不同种类的"containers"。如:有时我有一系列的动作应该继续执行,即使一个条目失败。在其他情况下,整个序列应立即停止。另一种风格是每个 Action 也有一个 undo()
方法的序列,这样当某些 Action 失败时,序列容器可以回滚所有先前(通过的)Action。
根据您的情况,您可能 "good to go",但我认为您至少应该考虑 what/if 您的个人任务可能 失败 ,以及如何您的 TaskProcessor 容器应该对失败的步骤做出反应。
在结构方面,这段代码是命令设计模式的应用。四人帮书中模式参与者的映射如下:
Task
是模式中的Command
接口,有它的execute
方法;Task1-3
为具体命令;TaskProcessor
就是Invoker
,也就是"asks the command to carry out the request"
但是,就意图而言,有点不符。四人帮书中所说的命令模式的初衷是
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
但是,问题 "Do you think this approach is OK for splitting a big method?" 表明目标是提供复杂计算的模块化分解,这是不一样的。