Five ‘s Weblog

November 17, 2007

The differences between Decorator Pattern and Proxy Pattern

Filed under: Five's thought — by powerdream5 @ 9:38 pm
Tags: ,

       Firstly, Let’s look at the following two UML diagrams which describes the basic implementation of Decorator Pattern and Proxy Pattern respectively.

11_17_2_2007.jpg

11_17_3_2007.jpg

        The two UML diagrams may confuse us. The two design patterns looks the same with each other. With Decorator Pattern, the decorator and the decoratee implement the same interfaces. With Proxy PatteRn, the proxy class and the real class delegated implement the same interfaces. Furthermore, no matter which pattern is used, it is easy to add functions before or after the method of the real object.        However, in fact, there are some differences between Decorator Pattern and Proxy 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. Therefore, when using Proxy Pattern, we usually create an instance of abject inside the proxy class. And when using Decorator Pattern, we typically pass the original object as a parameter to the constructor of the decorator.

       We can use another sentence to conclude thire differences: with the Proxy pattern, the relationship between a proxy and the real subject is typically set at compile time, whereas decorators can be recursively constructed at runtime.

//Proxy Pattern
public class Proxy implements Subject{

       private Subject subject;
       public Proxy(){
             //the relationship is set at compile time
            subject = new RealSubject();
       }
       public void doAction(){
             ….
             subject.doAction();
             ….
       }
}

//client for Proxy
public class Client{
        public static void main(String[] args){
             //the client doesn’t know the Proxy delegate another object
             Subject subject = new Proxy();
             …
        }
}
//Decorator Pattern
public class Decorator implements Component{
        private Component component;
        public Decorator(Component component){
            this.component = component
        }
       public void operation(){
            ….
            component.operation();
            ….
       }
}

//client for Decorator
public class Client{
        public static void main(String[] args){
            //the client designate which class the decorator decorates
            Component component = new Decorator(new ConcreteComponent());
            …
        }
}

11_17_4_2007.jpg

Advertisements

19 Comments »

  1. Good Explanation !! keep up the good work
    I look forward to more posts.

    Thank u

    Comment by kkevin13 — December 26, 2008 @ 4:02 pm |Reply

  2. Great! Thanks I love proxy pattern

    Comment by Alex — January 22, 2009 @ 12:34 am |Reply

  3. very good explanation, any pointers to other similar topics?
    -srihari konakanchi

    Comment by Srihari Konakanchi — May 17, 2009 @ 4:33 am |Reply

  4. it was very usefull,thanx alot,have a good time

    Comment by lila — May 20, 2009 @ 6:11 am |Reply

  5. my God, i thought you were going to chip in with some decisive insght at the end there, not leave it with ‘we leave it to you to decide’.

    Comment by ElenaLisvato — August 4, 2009 @ 12:28 pm |Reply

  6. Nice post. Please keep posting.

    Regards,
    Ketan Benegal

    Comment by Ketan Benegal — August 28, 2009 @ 1:55 am |Reply

  7. This was a Fantastic blog post, I will be sure to save this post in my Diigo account. Have a awesome evening.

    Comment by Chance Vanmatre — June 14, 2010 @ 5:52 am |Reply

  8. You you should make changes to the blog title The differences between Decorator Pattern and Proxy Pattern Five ‘s Weblog to more suited for your content you write. I liked the the writing withal.

    Comment by Seasons — October 30, 2010 @ 3:11 am |Reply

  9. You are correct in your post, perhaps seeing only consequences fo the difference: The difference primarily stands in the fact that Proxy is 100% transparent whereas Decorator is visible to the client code. In other words, decorator can have more methods than the original interface and client may want to call them. Proxy must not because the client does not know there is a proxy object and therefore there is no point having extra methods.
    For example: Reader and BuffredReader in Java. BufferedReader has an extra method readLine() which makes it a decorator as the client code might want to call this method.

    Comment by Ariel — November 27, 2010 @ 8:11 am |Reply

  10. Great Explanation. Another great article i recommend is:

    this

    Comment by Dharmendra Patel — April 7, 2012 @ 8:23 pm |Reply

  11. Do watch the above link , its really useful.

    Comment by Shaunak — May 26, 2012 @ 7:42 pm |Reply

  12. great post … very simple

    Comment by asif — February 24, 2013 @ 7:34 am |Reply

  13. you are actually a just right webmaster. The site loading
    velocity is amazing. It seems that you are doing any unique trick.
    Also, The contents are masterwork. you’ve performed a magnificent job on this matter!

    Comment by finger me — July 12, 2013 @ 7:40 am |Reply

  14. calvin klein 香水

    Comment by MIZUNO — October 4, 2013 @ 9:13 pm |Reply

  15. Perfect. The only explanation on the web that really explains this beautifully.

    Comment by james — September 19, 2014 @ 8:15 am |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: