Search code examples
design-patternsumlchain-of-responsibilitymicro-architecturedecorator

How can I add a decorator pattern to a chain of responsibility?


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


Solution

  • 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.