问题
I have a workflow that goes in sequence
A - > B - > C - > D - > E
I need a design pattern that allow me to add a state in between them with least code change.
http://en.wikipedia.org/wiki/Workflow_patterns
Which of these design pattern works?
回答1:
You could look into petri-net implementions, calculus-inspired frameworks like Jacob, virtual machines for processes like the PVM or a statemachine implementation like SCXML although the latter are waiting for state changes and then do something, so you need to sort of change your control flow into data flow.
If you want to implement it yourself, you need to make sure that you give the control back to some runtime controller instead of just calling the next node, because that would blow your stack. This runtime controller could also inject a context object into the activity runnables and that way you can share state between the activities. Please find a rough sketch as pseudo code below:
interface Activity {
Activity run(SharedContext context);
}
class A implements Activity {
public Activity run(SharedContext context) {
doA(context);
return new B();
}
}
class B implements Activity {
public Activity run(SharedContext context) {
doB(context);
return new C();
}
}
// runtime controller
SharedContext context = new SharedContext();
Activity next = new A();
while (next != null) {
next = next.run(context);
}
回答2:
You can try activiti. You can also design your own workflow using the eclipse plugin
来源:https://stackoverflow.com/questions/17829386/type-of-workflow-design-pattern-to-use