Preparing for Java Interview?

My books Grokking the Java Interview and Grokking the Spring Boot Interview can help

Download PDF

Difference between Proxy and Decorator Pattern in Java

Hello guys, if you have gone through a couple of Java Developer interview then you may seen this question about Proxy and Decorator Pattern in Java. Earlier, I have share 18 Design Pattern questions and today, I am going to answer this tricky design pattern questions from Java interviews. I was first asked about difference between Proxy and Design Pattern in Java a couple of years back when I was interviewing for Senior Java developer interview on a big investment Bank and I wasn't able to impress interviewer with my answer, so I decided to learn more and this article is the result of that research. Though both Proxy and Decorator pattern looks very similar to each other structurally, there are some key differences between them like what problem them solve and how they are used in Java application. 

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.


Similarity:

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:

1. Proxy Pattern

Here is the UML diagram of Proxy pattern which shows how can you implement them in Java with classes and interface.
Proxy Pattern in Java





2. Decorator Pattern

And, here is UML diagram of Decorator pattern which shows how it is applied and how it is structured in Java or any other object oriented programming language.

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. 

         
Other Java Design Pattern Tutorials and Examples you may like
  • 5 Free Courses to learn Object Oriented Programming (courses)
  • How to implement Command design pattern in Java? (Command pattern)
  • Difference between Factory and Dependency Injection Pattern? (answer)
  • 7 Best Courses to learn Design Pattern in Java (courses)
  • How to create thread-safe Singleton in Java (example)
  • How to implement the Strategy Design Pattern in Java? (example)
  • Difference between Factory and AbstractFactory Pattern? (example)
  • How to design a Vending Machine in Java? (questions)
  • Difference between State and Strategy Design Pattern in Java? (answer)
  • 20 System Design Interview Questions (list)
  • Top 5 Courses to learn Design Patterns in Java (design pattern courses)
  • How to use Adapter design pattern in Java (Adapter pattern)
  • 7 Best Books to learn the Design Pattern in Java? (books)
  • 5 Free Courses to learn Data Structure and Algorithms (courses)           

Thanks for reading this article so far. If you like my explanation of difference between Proxy and Decorator design pattern in Java then please share with your friends and colleagues. If you have doubt or any question feel free to ask in comments. 

P. S. - If you want to learn design pattern in Java and looking for best resources then you can also checkout these best Design pattern courses for Java developers to learn all object oriented design pattern in depth. 

No comments:

Post a Comment

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