Decorator Pattern
The Decorator Pattern is
(also known as Wrapper, an alternative naming shared with the Adapter pattern) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.
Decorator Pattern ensures
Modifying object functionality
var Product = function (name, brand, price) {
this.name = name;
this.brand = brand;
this.price = price;
}
Base Object
var ReturnedProduct = function (product, deliveryCost) {
this.product = product;
this.name = product.name;
this.brand = product.brand;
this.price = product.price;
this.deliveryCost = deliveryCost;
}
Decorator Object
Decorator Object Wrapping
Instance-creating with Base Class
var product = new Product ('shoes', 'adidas', 55, 5);
Instance-creating with Decorator Class
var decoratedProduct = new ReturnedProduct (product, 22);
A decorator example
var ReturnedProduct = function (product, deliveryCost) {
this.product = product;
this.name = product.name;
this.brand = product.brand;
this.price = product.price;
this.deliveryCost = deliveryCost;
}
var product = new Product ('shoes', 'adidas', 55, 5);
var decoratedProduct = new ReturnedProduct (product, 22);
Name of decorated product is
this.name = 'Shoes'
Brand of decorated product is
this.brand = 'Adidas'
Our new decorated object is initialized with
this.product = product
Price of decorated product is
this.price = 55
Delivery Cost of decorated product is
this.deliveryCost = 22
thanks
@hwclass
Copy of Decorator Pattern
By Barış SÖNMEZ
Copy of Decorator Pattern
- 326