DEV Community

loading...
Cover image for JavaScript design patterns: Decorators

JavaScript design patterns: Decorators

rahxuls profile image Rahul Updated on ・2 min read

I have explained about Object Creation pattern in JavaScript. Object creation pattern. So let's learn about the Decorator Pattern.

What is a decorator pattern?

The decorator pattern is a structural design pattern that attaches additional responsibilities to an object dynamically. This is a concept of adding extra functionality to the original structure.

However, keeping it decoupled and clean.

The decorator pattern adds new behaviour to objects dynamically at runtime wrapping itself around the original object. Multiple decorators can add or override functionality to the original object.


Example:

function Fruit() {
    this.cost = function () { return 100; }; 
}

function Orange(fruit) {
    var v = fruit.cost(); 
    fruit.cost = function () {
        return v + 75; 
    }
}

function Apple(fruit) {
    var v = fruit.cost(); 
    fruit.cost = function () {
        return v + 200; 
    }; 
}    

var fruit = new Fruit(); 
Orange(fruit); 
Apple(fruit); 
console.log('Total Cost', fruit.cost()); // Total Cost: 375
Enter fullscreen mode Exit fullscreen mode

It looks like Inheritance?

Inheritance and the decorator pattern allows changing object behaviour. But how they achieve this behaviour change is where inheritance and the decorator pattern are different.

The decorator would be difficult to implement when derived classes need to access non-public fields or methods in the parent class. The decorator pattern eliminates the problem of exploding class hierarchy encountered with inheritance.


Removal.AI - [SPONSOR]

Remove background from multiple images in a single upload straight to your desktop using Bulk Photo Background Remover for Windows.

  • ✅ Drag & Drop Multiple Images
  • ✅ Optimize Product Images
  • ✅ Select Your Background
  • ✅ Set Your Output Size
  • ✅ Exceptional Results

Visit -> Removal AI

Discussion (20)

pic
Editor guide
Collapse
atulcodex profile image
Atul Prajapati

Amazing post 😊

Collapse
alfredosalzillo profile image
Alfredo Salzillo 🐺

What you've liked about this wrong article? The part in which an Orange become an Apple?

Collapse
rahxuls profile image
Rahul Author

Respected sir, First of all I'm sorry if you didn't like this article.
I may have been mistaken somewhere while learning. Please help me in explaining.
I'm a learning a developer not like you a pro.
Explaining someone is way better than stalking someone and finding mistakes.

Thread Thread
alfredosalzillo profile image
Alfredo Salzillo 🐺

I've explained why you're wrong. You deleted the comment.
But I want to know what this guy liked of the article. An article how have a wrong content and a wrong example. I think they are only fake account that help you take visibility to be paid.
But quality matter too and this article is not good. It's explaining decorators in the wrong way.

Thread Thread
alfredosalzillo profile image
Alfredo Salzillo 🐺

And if you wanted to be explained you would reply to my comment. You deleted it because was a bad one and you need only good comment for sponsors.
I've reported you to the admins. I think quality comes first. I don't want in this community someone how post only for sponsors and post bad and misleading content.

Thread Thread
rahxuls profile image
Rahul Author

Sir i would tell you. I guess you don't know English. I sent you a task. And I ask apology for it.

Thread Thread
alfredosalzillo profile image
Alfredo Salzillo 🐺

You sent me a task?

Thread Thread
rahxuls profile image
Rahul Author

Sorry, I have a bad keyboard. I mean text.

Collapse
atulcodex profile image
Atul Prajapati

What I liked in this post ?
Ans. He tried to share his knowledge with others.
I believe in learning and sharing those knowledgs with other.

Collapse
patrickkoulalis profile image
Patrick Koulalis

Except what he has here isn’t correct.

Collapse
alfredosalzillo profile image
Alfredo Salzillo 🐺

This is all a scam. I hope the admins do something about this kind of situation.

Thread Thread
atulcodex profile image
Atul Prajapati

No problem, there is unlimited platforms who are looking for creators

Collapse
atulcodex profile image
Atul Prajapati

Mistakes are embedded feature in human nature 😌

Collapse
alfredosalzillo profile image
Alfredo Salzillo 🐺

Yeah, it's also in human nature fix something when you make a mistake, but this article is still here.
And I should assume that 28 other people are learning decorators the wrong way because of this.
There is nothing amazing going on here.
I'm going to write an article saying that the Earth is flat and I expect an amazing too because I'm sharing my knowledge.

Collapse
atulcodex profile image
Atul Prajapati

You are right, keep doing!
Go ahead

Collapse
patrickkoulalis profile image
Patrick Koulalis

These aren’t decorators

Collapse
atulcodex profile image
Atul Prajapati

you are right!

Collapse
atulcodex profile image
Atul Prajapati

Some buddy says my account is fake 🤣🤪

Collapse
alfredosalzillo profile image
Alfredo Salzillo 🐺

Really I don't know the 28 People that saved this article why they are saving a wrong article. This is all a skam.

Some comments have been hidden by the post's author - find out more