This program implements a simple software design pattern to orchestration complex processes and workflows. The application is showed by a tiny example.
Why the orchestrator pattern is useful?
- Simplifies implementation due to modularization of individual tasks.
- Allows you to easily execute tasks sequentially or in parallel.
- Easier understanding of the business logic.
- Faster development
In the Orchestration pattern, a single orchestrator manages the execution of the tasks of a complex process or workflow.
Integrating the Orchestrator pattern into existing code is easy:
- Create a data class for the context that contains all data that can be used by the tasks (in the following example this is
class MyContext). - For each task create a class that implements the interface
Task<MyContext>. - In your Main class instantiate the orchestrator object.
- Define the process that is executed by the orchestrator.
Let us assumed that your process or workflow has the following form:
flowchart LR
start((Start)) -->
TaskA[Task A] -->
TaskB[Task B] -->
TaskC[Task C] -->
stop((End))
Orchestrator orchestrator = new Orchestrator();
orchestrator
.setContext(new MyContext())
.sequence(List.of(new MyTaskA(), new MyTaskB(), new MyTaskC()))
;
Let us assumed that your process or workflow has steps that can be parallelized:
flowchart LR
start((Start)) -->
TaskA[Task A] --> TaskB[Task B] --> stop((End))
TaskA[Task A] --> TaskC[Task C] --> stop((End))
Orchestrator orchestrator = new Orchestrator();
orchestrator
.setContext(new MyContext())
.execute(new MyTaskA())
.setContext(new OtherContext())
.parallel(List.of(new MyTaskB(), new MyTaskC()))
;
UML diagramm
classDiagram
class Orchestrator {
setContext(Context context)
execute(Task~T~ task)
sequence(List~Task~T~~)
parallel(List~Task~T~~)
}
class Task~T extends Context~ {
<<interface>>
ececute(T context)
}
Orchestrator --> Task
Task --> Context
class Context
Orchestrator o-- Context
Task <|-- TaskA~Context1~
Task <|-- TaskB~Context2~
Context <|-- Context1
Context <|-- Context2
An example can be found in the package "example" of the code.