Consider, in an MVC architecture, you have the following action:
public String cancelMovement() {
try {
movementService.cancelMovement(movement);
} catch (AppException e) {
showMovement();
this.addActionError(getText(e.getKey()));
return INPUT;
}
return "saved";
}
Now the requiments change: : depending on movement.type, you should either just cancel the movement, or cancel movement + productService.revert(product) .
Question: What pattern would you use in this situation?
My considerations:
- I don't want to change the implementation of
movementService.cancelMovement(movement)
, as it violates the open/closed principle (and single responsibility) - In other situations I have used the decorator pattern (two different implementations of MovementService, one will cancel, the other will delegate to cancel and then revert(product). In this situation this doesn't seem appropriate because
- The MovementService has several other methdos (initiate, confirm,...) and a second implementation of MovementService wouldn't make sense for those methods.
- You would still need a place to determine which strategy to use
(if movement.status.equals(type1)... do this; else do that)
Any suggestions are welcomed!
Aucun commentaire:
Enregistrer un commentaire