locked
Decorator pattern vs. proxy pattern? RRS feed

  • Question

  • Could someone explain the difference between these two patterns?

     

    Both of them implements a certain API (via interface or inheritance from baseclass) an they both operate on a polymorphic-object (also implementing the same API).

     

    Is it that the decorator adds extra funtionality (extending the API) where the proxy seamlessly should look as it was an instance of the composed object/objects?

     

    //Daniel

    Monday, February 4, 2008 8:17 AM

Answers

  • Hi,

     

    Thie differences between the two patterns do not come from the fact that they both implement in a similar sort of a way, it is in their intent.

     

    A proxy pattern is used when you require one object to appear from the caller to be the object that they're expecting, but is actually only passing the functionality off to the real object.  A real world example can be found with remoting where a MarshalByRef object that lives on the server has a generated proxy object at the client that the calling code can call, that actually passes off the calls to the real object, that is out of process, on the server.

     

    The decorator object is used to add functionality to the object by injecting an instance of itself into a decorator object to perform the functionality.

     

    Your final statement is indeed correct.

     

    I hope that this helps,

     

    Martin Platt.

    Tuesday, February 5, 2008 3:40 AM

All replies

  • Hi,

     

    Decorator and Proxy have similarities. The difference lies in the fact that in Proxy pattern, the association of subject and proxy is typically set at compile time. In Decorator, it can be constructed at runtime. Decorator focuses at adding functionality dynamically to an object, Proxy focuses on controlling access to an object.

     

    useful link:

    http://powerdream5.wordpress.com/2007/11/17/the-differences-between-decorator-pattern-and-proxy-pattern/

     

     

    HTH,
    Suprotim Agarwal

    -----
    http://www.dotnetcurry.com
    http://www.sqlservercurry.com
    -----

    Monday, February 4, 2008 8:32 AM
  • Hi,

     

    Thie differences between the two patterns do not come from the fact that they both implement in a similar sort of a way, it is in their intent.

     

    A proxy pattern is used when you require one object to appear from the caller to be the object that they're expecting, but is actually only passing the functionality off to the real object.  A real world example can be found with remoting where a MarshalByRef object that lives on the server has a generated proxy object at the client that the calling code can call, that actually passes off the calls to the real object, that is out of process, on the server.

     

    The decorator object is used to add functionality to the object by injecting an instance of itself into a decorator object to perform the functionality.

     

    Your final statement is indeed correct.

     

    I hope that this helps,

     

    Martin Platt.

    Tuesday, February 5, 2008 3:40 AM
  • Hi,

    I am not sure how these two patterns are being considered for comparsion here because their actual intents are quite different.

    Proxy usually use in the context where object/objects are created on behalf of another real object which is normally inaccessible directly. The reasons for inacessiblity could be because the real object is existed in another process boundary for instance. Moreover, it would require to write lots of plumbing code to access the real object on the another process boundary and that has to be abstracted out of end user perspectives.

    On the other hand, Decorator is all about decorating the behaviour of an object or group of objects. Object could be decorated at runtime or compiled time but the power of decorator would be manipulated at run time anyway. A good example of compiled decoration in .NET are System.Attributes. System.ComponentModel is another example for runtime.

    Hope it helps!

    Priyo     


    Thursday, February 7, 2008 11:56 AM