Firstly, Let’s look at the following two UML diagrams which describes the basic implementation of Decorator Pattern and Proxy Pattern respectively.
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());
…
}
}
Good Explanation !! keep up the good work
I look forward to more posts.
Thank u
Comment by kkevin13 — December 26, 2008 @ 4:02 pm |
Great! Thanks I love proxy pattern
Comment by Alex — January 22, 2009 @ 12:34 am |
Comment by alexsmith11 — February 22, 2009 @ 12:03 pm |
very good explanation, any pointers to other similar topics?
-srihari konakanchi
Comment by Srihari Konakanchi — May 17, 2009 @ 4:33 am |
it was very usefull,thanx alot,have a good time
Comment by lila — May 20, 2009 @ 6:11 am |
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 |
Nice post. Please keep posting.
Regards,
Ketan Benegal
Comment by Ketan Benegal — August 28, 2009 @ 1:55 am |
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 |
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 |
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 |
Great Explanation. Another great article i recommend is:
this
Comment by Dharmendra Patel — April 7, 2012 @ 8:23 pm |
http://thecafetechno.com/tutorials/design-patterns/proxy-pattern-in-java/
Comment by Dharmendra Patel — April 7, 2012 @ 8:23 pm |
check this article:
http://thecafetechno.com/tutorials/design-patterns/proxy-pattern-in-java
Comment by Dharmendra Patel — April 7, 2012 @ 8:24 pm |
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
Comment by Shaunak — May 26, 2012 @ 7:41 pm |
Do watch the above link , its really useful.
Comment by Shaunak — May 26, 2012 @ 7:42 pm |
great post … very simple
Comment by asif — February 24, 2013 @ 7:34 am |
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 |
calvin klein 香水
Comment by MIZUNO — October 4, 2013 @ 9:13 pm |
Perfect. The only explanation on the web that really explains this beautifully.
Comment by james — September 19, 2014 @ 8:15 am |
Uffff…… Finally I know the difference ….
Thanks a lot !
Comment by Sanjay — November 12, 2018 @ 1:46 am |
excellent. very very helpfully. tanks
Comment by pouya lariyan — September 18, 2019 @ 3:17 am |
Thanks for your concise and perfect explanation!
Comment by Enrigle — November 9, 2021 @ 12:50 am |