Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

Difference between Proxy and Decorator Pattern in Java

Though both Proxy and Decorator pattern looks very similar to each other structurally, there are some key differences between them. The main difference between Decorator and Proxy patterns comes from their Intent. Proxy word has a long-standing meaning and in software world also a Proxy class stands for its implementation class, which presents lots of options to the programmer, resulting in different kinds of proxy objects e.g. remote proxy, virtual proxy, and protection proxy. On the other hand, Decorator design pattern is used to add new functionality into an existing object without modifying its class. As we have seen in the previous example how you can use Decorator to add ingredients into Pizza or BufferedReader which adds buffering functionality into Reader class.

So their usage is entirely different. Here is some notable difference between them :

- Proxy is used to control access while Decorator is used to adding additional functionality without modifying the class itself.

- In case of Proxy pattern, Proxy class hide details of implementation class while in case of Decorator, implementation class is exposed to the client.

- In case of Proxy, the relationship between implementation and Proxy is known at compile time but in case of Decorator, implementation class is passed at runtime.


1) Both Proxy and Decorator implementations same interface as the actual class, so that they can stand in their places e.g. passed to a method which accepts actual class or can return from a method which returns an interface of the actual class.

2) Both use delegation to get things done.

3) both pattern allows you to add new functionality before and after calling the method on implementation class.

Now, let's see their UML diagram to spot the difference:

Proxy Pattern

Proxy Pattern in Java

Decorator Pattern

Decorator Pattern in Java

That's all about the difference between Proxy and Decorator design Pattern in Java or Object-Oriented Programming. If you like this kind of short, to-the-point article then do let me know as your feedback is important to me. These are easier to create for me and I think its easier for you also to learn and understand key concepts. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.