Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Im reading "Head first design patterns" (here you can find exact example mentioned in book)

While going through Decorator design pattern, everything was crisp and clear until i stumbled into this

they are explaining decorator using

  1. Extensions I dont think extending a class through 'Extensions' results in decorator design pattern since we cant add properties to classes. As I understand, key to decorator is it ability to wraps other objects infinity, this is not possible here, although we can add functionality to existing class without inheriting it.

  2. Delegation Delegation here is completely out of context. Isnt delegation itself a design pattern?

share|improve this question
    
I can comment on your second question: Decorator uses Delegation(pattern) in a very specific way. i.e., Delegation is a general way to build complex behaviour by coordinating use of several other objects, where as Decorator is a dynamic way to use delegation to add behaviour to a logical entity at runtime. –  FaizanRabbani Jan 12 at 9:25

2 Answers 2

up vote 2 down vote accepted

I think the book “Head first design patterns” is correct.

  1. As per Wikipedia, this pattern “allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class”. 'Extensions' are adding capability to an object statically and behaviors can be added by adding methods only, it may not need a new property (that is not a compulsory condition here). This stackoverflow question discussed similar stuff.

  2. Delegation can be considered as a pattern and can be utilized to add a behaviors to an existing object as per decorator pattern. Have a look on following links: http://stackoverflow.com/questions/3929996/gof-explanation-of-the-decorator-pattern-is-confusing-or-just-plain-wrong and http://lgiordani.com/blog/2014/05/19/method-overriding-in-python/

Hope it will help. Thanks.

share|improve this answer
    
thanks, things are clearing up now –  Saqib Saud 2 days ago
    
welcome Saqib :-) –  Snesh 2 days ago

You cannot directly add attributes and methods in a class, but you can do that in the decorator. Since a decorator is a composition of its supertype, you can state that the decorator is a basic object, with additional methods and attributes. Exactly like a sub-class is its super-class, with additional or modified methods and attributes.

In typical decorator, a part of the behaviour is simply identical to what id defined in the associated basic object. So you implement the methods reifying this behaviour by simply call the same method in the associated object. That's why you can say that the Decorator Design Pattern can be used as a delegation mechanism.

It is relatively common to find (a part of) a pattern in an other one. You could for instance consider the decorator as a special kind of proxy.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.