我正在开发的本地工作流框架为每个需要调用以执行工作流的任务实例化一个类。更多任务=要创建的更多类。如何优化?我不想为每一项任务创建一个班级。
例如
Class MyWorkflow {
public List < Task > getTasks() {
return new ArrayList < Task > (
Arrays.asList(new Task1(),
new Task2(),
new Task3(),
)
);
}
}
答案 0 :(得分:0)
如果支持一个常规任务过于通用,则可能有一个Task或至少几个已定义的Tasks类。 Varargs允许您根据操作要求传递实体ID等。或者传递Employee.class而不是object?
public class TaskRunner {
public List <Task> getTasks() {
return new ArrayList <Task> (
Arrays.asList(new Task(new Employee(), TaskOperation.Delete, "10"),
new Task(new Employee(), TaskOperation.Retrieve, "15")
)
);
}
}
public enum TaskOperation {
Create,
Delete,
Update,
Retrieve;
}
public class Task implements Runnable {
private final Object entity;
private final TaskOperation operation;
public Task(Object entity, TaskOperation operation, String... args) {
super();
this.entity = entity;
this.operation = operation;
}
@Override
public void run() {
// EmployeeDAO employeeDAO = ...
if (entity instanceof Employee) {
switch(operation) {
case Create:
// Perform Create on Employee
// Employee e = new Employee();
// employeeDAO.create(...);
break;
case Delete:
// Perform Delete on Employee
break;
}
// ....
}
}
}