Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Decorator pattern and Delegation pattern

What is the difference between Decorator pattern and Delegation pattern (if there is any) ? I don't want to know just about implementation details but also about use case differencies and subjective point of view how to use them.

  • Decorator pattern
  • Delegation pattern

EDIT : Can you point to source code (in OS project) where these pattern (especially Delegation, because Decoration is used in Java IO classes) are used. I'm looking for some real usage not just dummy example. Maybe these patterns are the same differs only in title. Feel free to write this opinion.

like image 500
michal.kreuzman Avatar asked Nov 15 '12 00:11

michal.kreuzman


People also ask

What is the difference between decorator and Strategy pattern?

The strategy pattern allows you to change the implementation of something used at runtime. The decorator pattern allows you augment (or add to) existing functionality with additional functionality at run time.

What is the difference between the proxy and the decorator pattern?

Decorator Pattern focuses on dynamically adding functions to an object, while Proxy Pattern focuses on controlling access to an object. In other words, with Proxy Pattern, the proxy class can hide the detail information of an object from its client.

Is delegation a design pattern?

In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance. In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context.

What is the main difference between the composite and the decorator patterns?

Composite is intended to combine and represent multiple objects as a single one (of the same base type) - i.e. 1 to Many, while Decorator enhances (or adds on top of) the capability of a single object of the same type - i.e. 1 to 1.


1 Answers

Decorator uses Delegation, but in a very specific way.

Delegation (or composition) is not as much a pattern as a general way to build complex behavior by coordinating use of several other objects. It's often used in a set or static way. By "set or static" I mean something like this:

class Delegator {   private final ClassA a = new ClassA();   private final ClassB b = new ClassB();    public void doWork() {      a.setup();      final ResFromA resa = a.getRes();      b.setup();      b.consume(resa);   }  } 

Note that Delegator does not share any type or interface with either ClassA or ClassB, and knows the exact type of both a and b.

Decorator is a dynamic way to use delegation to add behavior to a logical entity at runtime. In Decorator all entities share a common interface, and use delegation to concatenate their work.

public interface Item {   public void drawAt(final int x, final int y); }  public class CircleAround implements Item {   private final Item wrapped;   private final int radius;    public CircleAround(public final Item wrapped, public final int radius) {     this.wrapped = wrapped;     this.radius = radius;   }    public void drawAt(final int x, final int y) {     // First handle whatever we are wrapping     wrapped.drawAt(x,y);     // Then add our circle     Graphics.drawCircle(x, y, radius);   }  } 

Note that unlike the first example, CircleAround does not know the exact type of the item that it wraps, and shares a common interface with it.

like image 127
Anders Johansen Avatar answered Sep 22 '22 21:09

Anders Johansen