Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

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. 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 programmer, resulting in different kinds of proxy objects like remote proxy, virtual proxy, and protection proxy. On the other hand, Decorator pattern is used to add new functionality into existing object without modifying it's class. So their usage is entirely different.

Difference between Proxy vs Decorator Pattern in Java

Here are some notable difference between them :

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

2. In case of Proxy patter, Proxy class hide details of implementation class while in case of Decorator, implementation class is exposed to client.

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


Difference between Proxy and Decorator Pattern in Java

Proxy and Decorator Pattern in Java - Similarties

Now, let's checkout some similarities between Proxy and Decorator Pattern in Java:

1. Both Proxy and Decorator implements same interface as actual class, so that they can stand in their places like passed to a method which accept actual class or can return from a method which returns interface of actual class.

2. Both uses delegation to get things done.

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

Decorator vs Proxy pattern in Java

And, if you want to learn more about object oriented design Patterns, here is a Java design pattern cheatsheet to take print and study

Java Design Pattern cheatsheat

Other Java Design Patterns tutorials you may like

  • 5 Free Courses to learn Object Oriented Programming (courses)
  • 18 Java Design Pattern Interview Questions with Answers (list)
  • Difference between Factory and Abstract Factory pattern in Java (answer)
  • How to design a Vending Machine in Java? (questions)
  • 20 System Design Interview Questions (list)
  • Difference between Factory and Dependency Injection Pattern? (answer)
  • 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 get a method of HashMap to work internally in Java? (answer)
    Difference between Abstraction and Encapsulation in Java? (answer)
  • Difference between State and Strategy Design Pattern in Java? (answer)
  • Top 5 Courses to learn Design Patterns in Java (courses)
  • 5 Free Courses to learn Data Structure and Algorithms (courses)

Thanks a lot for reading this article so far. If you like this Decorator vs Proxy design pattern tutorial in Java then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you want to learn more about design patterns and how they can help you to write better code in Java, I suggest you take a look at the Java Design Patterns and Architecture - a Free course on Udemy.

No comments:

Post a Comment

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