“Decorator pattern attach additional functionalities or responsibilities to an existing object at the runtime”. It adds new behaviour to an individual object without affecting other objects of the same class. New behaviour or functionalities can be either added dynamically at the run time or statically.
The decorator pattern:
- · Adds new functionality or behaviour
- · Either at run time or statically
- · Without affecting other objects of the class
Why do we need this pattern?
To understand, let us consider a Student object with the basic responsibility to handle and print information like name, age, and grade etc.
Now you have a requirement to add functionality to maintain lab reports for Science students. How will you do this? Maybe extend the Student class to create a ScienceStudent subclass for this responsibility. Good enough.