Lesson 3 - The Decorator Pattern
The Decorator Pattern (also known as the wrapper) is a Structural Design Pattern. Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient.
It allows us to dynamically add properties to objects at runtime. The goal is to combine a single concrete class with one or more Decorators.
Definition
Decorator is a structural design pattern that lets you attach new behaviours to objects by placing these objects inside special wrapper objects that contain the behaviours.
The Decorator Pattern allows behaviour to be added to an individual object , either statically or dynamically without affecting the behaviour of other objects in the same class. - Wikipedia
The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. - Head First Design Patterns
Provides an alternative to subclassing for extending functionality
When to use ?
The Decorator pattern is a good pattern to use when there may be many variations of an object. Use the pattern when it’s awkward or not possible to extend an object’s behavior using inheritance.
For example:
-
You want to keep track of many different Teslas’s and each Tesla Model can have multiple Features, thus affecting the cost.
-
You have many different computers, each computer having different parts , and you want to keep track of what functions can or cant be done based on the parts of each pc.
-
You have many different types of coffees, each coffee can have any variation of ingredients affecting the cost.
Important to Note
-
This pattern offers an innovative way to extend objects while honoring the open-closed principle. (A class should be open for extension but closed for modification)
-
Supports the Single Responsibility Principle as it allows functionality to be divided between classes with unique areas of concern.
-
Decorators have the same supertype as the objects they decorate.
-
You can use one or more decorators to wrap an object.
-
Given that the decorator has the same supertype as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object.
-
The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job.
-
Objects can be decorated at any time, so we can decorate objects dynamically at runtime with as many decorators as we like.
-
One of the downsides of the Decorator Pattern: designs using this pattern often result in a large number of small classes that can be overwhelming to a developer
Benefits
Model list is not polluted with models that are not required.
You can extend an object’s behavior without making a new subclass.
You can add or remove responsibilities from an object at runtime.
You can combine several behaviors by wrapping an object into multiple decorators.
Single Responsibility Principle. You can divide a monolithic class that implements many possible variants of behavior into several smaller classes.
Code Examples
Links
https://www.youtube.com/watch?v=WPOLDEk1LF0 - Tesla Example https://www.youtube.com/watch?v=1rjpesD2wl8 - Computer Example https://www.youtube.com/watch?v=6PPMR0GWrZQ - Car and Coffee Example https://refactoring.guru/design-patterns/decorator https://www.codeproject.com/Articles/479635/UnderstandingplusandplusImplementingplusDecoratorp https://www.javacodegeeks.com/2015/01/how-the-decorator-pattern-saved-my-day.html https://dzone.com/articles/decorator-design-patterns#:~:text=Decorators%20are%20used%20when%20we,an%20entire%20class%20of%20objects.
Comments
Post a Comment