samedi 21 avril 2018

Which pattern would you suggest for "do different things depending on configuration"?

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:

  1. I don't want to change the implementation of movementService.cancelMovement(movement), as it violates the open/closed principle (and single responsibility)
  2. 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
  3. The MovementService has several other methdos (initiate, confirm,...) and a second implementation of MovementService wouldn't make sense for those methods.
  4. 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