Skip to main content

The Decorator Pattern

--- ---

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:

  1. You want to keep track of many different Teslas’s and each Tesla Model can have multiple Features, thus affecting the cost.

  2. 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.

  3. You have many different types of coffees, each coffee can have any variation of ingredients affecting the cost.

Important to Note

  1. 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)

  2. Supports the Single Responsibility Principle as it allows functionality to be divided between classes with unique areas of concern.

  3. Decorators have the same supertype as the objects they decorate.

  4. You can use one or more decorators to wrap an object.

  5. 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.

  6. The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job.

  7. Objects can be decorated at any time, so we can decorate objects dynamically at runtime with as many decorators as we like.

  8. 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

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

Popular posts from this blog

The Factory Pattern

Lesson 1 - The Factory Pattern In this series we will be going through the various design patterns that are available. I will be using many different sources to gather information and all these sources will be listed in the links below. I will also be heavily referencing from the Head First Design Patterns Book which is definitely worth a read. At the bottom of each article there will also be links to some code examples for you to go through and play around with. So lets begin.  The Factory Pattern is a Creational Design Pattern. Creational Design Patterns are design patterns that deal with object creation mechanisms. In this tutorial we will look at the following : Simple Factory Factory Method. Abstract Factory. Definition Factory Method “The Factory Method Pattern defines an interface for creating an object but lets the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to sub classes.” - Head First Design Patterns (Page 134...

Design Patterns

Design Patterns The purpose of this blog is to work through a few design patterns and note the important aspects of each. I will also be updating each article with relevant links to videos and articles that have helped me in my learnings.  Some of the patterns we will look at include:   The Factory Method  The Abstract Factory  The Strategy Pattern  The Decorator Pattern  Apart from the important aspects of each of the Design Patterns as well as the useful links to resources I will also include code examples from my bitbucket that will assist in providing a hands-on experience. The code demonstrating these patterns is written in Typescript.