Combine a controller and dispatcher with views and helpers to handle client requests and prepare a dynamic presentation as the response. Controllers delegate content retrieval to helpers, which manage the population of the intermediate model for the view. A dispatcher is responsible for view management and navigation and can be encapsulated either within a controller or a separate component.
Real world example
In the classic MVC pattern, M refers to the business model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different forms of expression. In the Service to Worker pattern, the C directly controls the display of the V and can receive commands to control the dispatcher indirectly. The dispatcher stores different commands that can be used to modify the
model
withaction
s or to modify the display in theview
s.
In plain words
Service to Worker Pattern uses Dispatcher to combine the controller and the view to handle client requests and prepare a dynamic presentation as the response.
Programmatic Example
We modified this pattern based on a classic design patterns Model View Controller Pattern as the Class Diagram and two main classes Dispatcher
and Action
have been added.
The Dispatcher, which encapsulates worker and view selection based on request information and/or an internal navigation model.
1public class Dispatcher {
2
3 private final GiantView giantView;
4 private final List<Action> actions;
5
6 /**
7 * Instantiates a new Dispatcher.
8 *
9 * @param giantView the giant view
10 */
11 public Dispatcher(GiantView giantView) {
12 this.giantView = giantView;
13 this.actions = new ArrayList<>();
14 }
15
16 /**
17 * Add an action.
18 *
19 * @param action the action
20 */
21 void addAction(Action action) {
22 actions.add(action);
23 }
24
25 /**
26 * Perform an action.
27 *
28 * @param s the s
29 * @param actionIndex the action index
30 */
31 public void performAction(Command s, int actionIndex) {
32 actions.get(actionIndex).updateModel(s);
33 }
34
35 /**
36 * Update view.
37 *
38 * @param giantModel the giant model
39 */
40 public void updateView(GiantModel giantModel) {
41 giantView.displayGiant(giantModel);
42 }
43}
The Action (Worker), which can process user input and perform a specific update on the model.
1public class Action {
2
3 private final GiantModel giant;
4
5 /**
6 * Instantiates a new Action.
7 *
8 * @param giant the giant
9 */
10 public Action(GiantModel giant) {
11 this.giant = giant;
12 }
13
14 /**
15 * Update model based on command.
16 *
17 * @param command the command
18 */
19 public void updateModel(Command command) {
20 setFatigue(command.getFatigue());
21 setHealth(command.getHealth());
22 setNourishment(command.getNourishment());
23 }
24}
Therefore, this example leverages the Service to Worker pattern to increase functionality cohesion and improve the business logic.