FuzzDuck
FuzzDuck
Decorator pattern
FuzzDuck
Decorator pattern

Decorator pattern

The Decorator pattern gives a way of adding responsibility dynamically to objects. With this pattern you can add values to variables from one class to another. For example, you can have an object, which you wish to decorate. Through the Decorator pattern, we have a ConcreteComponent, to which we can add different decorations. The decorations can change the value of the ConcreteComponent’s parameters, both string and numeric values. For example, a ConcrecteComponent could resemble a product, which has its cost and product description changed as more decorations are applied. The different decorations add to the price of the original object, thus the different decorators have a method to add to the total cost. The decorations also implies on the product description to tell, which decorations are added.

Decorator Pattern UML

The car components

A customer comes up to a car retailer and orders a car. The specific car cost a default amount of of $1500. The customer decides, that he wants new tires ($300), tv in the backseats ($150), and car stereo ($200). With the decorator pattern we have an object of the ConcreteComponent (the car), which we link with additional ConcreteDecorators (The tires, tv, and car stereo). Through this pattern we can add all the car components we want, and just add up to the total price and orderlist.