0

Let's say we have a component class like this:

class Component:
    def operation(self) -> str:
        return f"Performing operation"

    def another_operation(self) -> str:
        return f"Performing another operation"

We then have a child of component that overrides both of its methods:

class ChildComponent(Component):
    def operation(self) -> str:
        return f"Performing operation differently"

    def another_operation(self) -> str:
        return f"Performing another operation differently"

We may then define a decorator that modifies the behaviour of operation:

class Decorator(Component):
    _component: Component = None

    def __init__(self, component: Component) -> None:
        self._component = component

    def operation(self) -> str:
        return f"Decorated...({self._component.operation()})"

    def another_operation(self) -> str:
        return self._component.another_operation()

From my understanding, even though we are not modifying the behaviour of another_operation() in the decorator, we must still define it rather than rely on the superclass method otherwise Component's another_operation() will be called instead of ChildComponent's method, and you'd have a bad hybrid situation on your hands.

If we were to do that however, then any time the Component class gained a new method, we'd have to add it to the decorator too, which isn't in keeping with the Interface Segregation Principle. So we either have to violate SOLID principles and maintain twice the amount of code that we need to, or risk using the wrong methods for those we don't explicitly override in the decorator.

Could someone please provide some clarity around this?

Samaruman
  • 21
  • 4
  • 1
    Be aware that Python allows you to implement the Decorator pattern in such a way that it forwards operations automatically, using e.g. ``__getattr__``. Are you asking about the Decorator pattern *in general*, or rather the implementation shown here? – MisterMiyagi Mar 23 '21 at 11:02
  • I am asking generally but could you link me to some more information on this forwarding please? I'm still adapting to Python from Java. – Samaruman Mar 23 '21 at 11:19
  • See [Implementing the decorator pattern in Python](https://stackoverflow.com/questions/3118929/implementing-the-decorator-pattern-in-python) for an example using ``__getattr__``. – MisterMiyagi Mar 23 '21 at 11:22
  • Adding a method to `Component` violates the Open/Closed Principle, whether it has a Decorator or not. – jaco0646 Mar 23 '21 at 14:24

1 Answers1

0

I'm marking this question as resolved due to the comment on my original question above about the fact adding a method to the class violates SOLID principles regardless.

Samaruman
  • 21
  • 4
  • Just mark the question as deleted to be sure that you don't get in trouble for that – Staxlinou Oct 25 '21 at 09:51
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/late-answers/30169826) – Emi OB Oct 25 '21 at 10:38