Decorator pattern(structual)

  • Dynamically adding behaviors to exist objects
  • Aggregation(Containing old objects)

The solution with the Decorator pattern

def addSMS(cls):

    class AddSMS(cls):

        def send(self, message):
            super().send(message)
            print("SMS sent")

    return AddSMS

def addFackbook(cls):

    class AddFackbook(cls):

        def send(self, message):
            super().send(message)
            print("Fackbook sent")

    return AddFackbook

@addSMS
@addFackbook
class Notifier:
    def send(self, message):
        print("Email sent")

Notifier().send("123")
Written on June 18, 2021