Decorator Method

Decorator Method

Decorator Method

Decorator method is a type of structural design pattern that lets us assign new behaviours to objects by storing these objects inside unique wrapper objects that carry the actions. This pattern helps in attaching functionality to a class without altering the class itself. The “Wrapper” is the alternative nickname for the Decorator pattern that clearly shows the central idea of the design. 

Decorator method creates a class that wraps the original class and provides additional functionality for keeping the class methods signature intact. Through this, it attaches other responsibilities to the object dynamically. The decorator method has a structure it follows and they are:

  • Base Decorator Class
    • It’s a class that has a field of referencing wrapped object.
    • It assigns all operation to the wrapped object.
  •  Client
    • it wraps the component in multiple layers of decorators.
  • Component
    • it defines the standard interface for both wrappers and wrapped objects.
  • Concrete Decorators,
    • it describes different behaviours that can be added to elements dynamically.
  • Concrete Component Class
    • It’s a class of objects wrapped and explains the necessary behaviour which can be altered by decorators.



The decorator method of the structural design pattern implementation uses the following guidelines:

  • Choose a domain that has a primary component with multiple optional layers.
  • Highlight methods common to both primary component and optional layers
  • Create and define a concrete component class with the base behaviour in it.
  • Create a base decorator that has a field for keeping a reference to a wrapped object.
  • Check that all classes implement the component interface well enough.
  • Create concrete decorators though the means of extending them from the base decorator.
  • The client(user) code must be responsible for creating and composing decorators in a way the client(user) needs it.
Code Implementation

During the cold season, one tends to feel cold through that it gets cold as times goes on. Wearing thick clothes or sweaters could help you surpass the cold season. So, wearing clothes is an excellent example of a decorator method because when you feel cold, you wrap yourself with a sweater if the cold persists you can wear a jacket on the sweater if the cold lasts you can extend to put on a garment.

The python code used to implement the above. Illustration:

# The Component(base component)

class WearingClothes:

    """ Represents a written description of the client"""

    def __init__(self, text):

        self._text = text

    def outfit(self):

        return self._text

class Sweater(WearingClothes):

    """ Represents a written description of the client after the cloth """

    def outfit(self):

        return "I'm still cold."

class Jacket(WearingClothes):

    """ Represents a written description of the client after the sweater """

    def outfit(self):

        return "I feel a bit better but still need a some warm cloth"

class Garment(WearingClothes):

    """ Represents a written description of the client after the Jacket"""

    def outfit(self):

        return "I feel better now."

if __name__ == '__main__':

    # When your are feeling cold

    new_cloth = WearingClothes("I'm feeling Cold")

    # Wear a Sweater

    sweater = Sweater(new_cloth)

    # Wear a Jacket

    jacket = Jacket(sweater)

    # Wear a Garment if the cold still persist.

    garment = Garment(jacket)

    print("before : {} \n".format(new_cloth.outfit()))

    print("after wearing a sweater: {} \n".format(sweater.outfit()))

    print("after wearing a jacket: {} \n".format(jacket.outfit()))

    print("after wearing a garment: {}".format(garment.outfit()))

Code Output:

before : I'm feeling Cold

after wearing a sweater: I'm still cold.

after wearing a jacket: I feel a bit better but still need a some warm cloth

after wearing a garment: I feel better now.

Pros and Cons

  • Single Responsibility Principle
  • SubClassing
  • Runtime Responsibilities
  • Complicated decorators
  • Wrapper removal
  • Tedious Configuration