1

I created the following UML. It's basically a currency converter. As it is now, it is a Chain of Responsibility. But now I want to add a Decorator pattern. So, for example, add a fixed processing amount. How can I insert a Decorator pattern here? Thanks for the help!

COR

BogoBogo
  • 171
  • 2
  • 6
  • It could help to visually show the chain with a reflective association having a `next`role at one end instead of hiding the WR in the attributes ;-) – Christophe Jan 03 '22 at 11:07

1 Answers1

1

The chain of responsibility aims to give to more than one object (instances of WR specializations) the possibility to handle a request, here the umrechnen() operation. A decorator is meant to add extra responsibility, such as computing some fixed transactional fees.

Several solutions can be considered, depending on your intent:

  • Adding responsibility to the whole chain: the decorator implements/realizes the IUmrechner interface and refers to an IUmrechner element (either another decorator, or the first handler of the chain.
  • Adding responsibility to handlers: the decorator extends/specializes WR and refers to a WR. This seems very flexible, but makes the chain cumbersom to populate. Moreover, this works well with the chain, only if the added responsibility can be wired into the umrechnen() request: otherwhise the chain could not exploit this responsibility.

A third approach that is worth to explore is hybrid:

  • change your WR to make the next an IUmechnen instead of a WR. Because in reality you do not need to know how the next computes the result if the current is not the appropriate one. In this case, insert your decorator at the level of the interface. You then have the choice at runtime, if you want to insert one global decorator ahead of the chain, of if you want to insert some decorators for some handlers.
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • would that be feasible? [UMLwithDecorator](https://i.ibb.co/ZXq4GKH/image.png) – BogoBogo Jan 03 '22 at 12:54
  • 1
    @BogoBogo yes, this is the first approach proposed. The client refers to an IUmrechner. you could provide the client a WR with a whole chain, or you could provide to the client a decorator that would rely on an iumrechner that happens to be assigned to a WR instance. – Christophe Jan 03 '22 at 13:16