Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, September 7, 2021

Adapter vs Decorator vs Facade vs Proxy Design Pattern in Java

There is some striking similarity between Adapter, Decorator, Facade, and Proxy design patterns, in the sense that they all use Composition and delegation to solve the problem. The adapter pattern wraps an interface, and delegates call to it. The decorator wraps an object and implements behavior on top of that, Facade wraps one or more interfaces to provide a central interface, which is easy to use and Proxy Pattern also wraps Subject and delegates calls to it. Then questions come, why are different patterns? What is the difference between Adapter, Decorator, Facade, or Proxy pattern, if their structure is the same. The answer is Intent. Yes, all of these Java design patterns have similar structure and class diagrams but their intents are totally different from each other.

The main purpose of the Adapter pattern is to convert interfaces. The Adapter Pattern lets two components working together, which wouldn't be working because of incompatible interfaces. The decorator pattern, adds new functionalities at runtime.

It allows you to enrich the object, even after its creation. Facade design pattern neither converts interfaces nor adds new functionality, instead, it just provides simpler interfaces.

So instead of the client directly accessing individual components of a system, it uses a facade. Facade design pattern allows clients to interact with complex systems with a much simpler interface and less work. The facade will then call individual components. The proxy pattern is also quite similar to the Adapter and Decorator, but its purpose is to control access of objects.

Proxy prevents clients to directly access objects, instead, it acts as a real object and can provide alternate behavior or forward request to the original object.

Proxy is the most versatile pattern of all these, and it can be used in different ways like a remote proxy for communicating with a remote object, virtual proxy to control access of expensive object, protection proxy to provide access to objects based on roles, Caching proxy, which can return cached object etc. In this Java design pattern article, we will try to examine some similarities and differences between Adapter vs Decorator vs Facade vs Proxy patterns in Java.

Btw, If you are serious about learning design patterns and principles, I suggest you take a look at the Design Patterns in Java course on Udemy.  This course covers both SOLID design principles like Open Closed and Liskov substitution, and all-important Object Oriented design patterns like Decorator, Observer, Chain of Responsibility, and much more.

Similarities between Adapter, Facade, Proxy, and Decorator Pattern in Java

One of the major similarities is there structure, all of these Pattern wraps an object and delegate request processing or method call to them. Here are few more things, which is common between all of these patterns.

1) All of them are defined as structural patterns as GOF design pattern in there classic book Java Design Patterns.

2) All of them uses Composition and delegation to fulfill there intent. Adapter uses composition to forward calls from target interface to adaptee interface, Decorator also uses same technique before adding new behavior, Facade is composed of all sub components and Proxy also use composition and delegation to forward requests.

3) Both Decorator and Proxy pattern is meant to stay in a place of the original object, that's why both Decorator and Proxy pattern implements the interface of a real object. Because of this nature, a decorator and proxy can be passed to a method, which accepts an original or real objects.

4) Both Adapter and Facade pattern, forwards requests to a different interface, which can be either adaptee or any component interface from sub-system.

5) Another similarity between the Adapter and Facade patterns is that they can wrap multiple interfaces, which is also different from than Decorator and Proxy patterns because they tend to operate on one object.

Difference between Adapter vs Decorator vs Proxy vs Facade design Pattern in Java

Now we know that there are a lot of similarities between all of these structural patterns, let's see some differences between them. As I have said earlier, they differ in their intent, what problem they solve and where to use them.

1) Adapter pattern converts interface, Decorator pattern doesn't convert interface, it just implements original object's interface, so that it can be passed to a method, which accepts an original object. Facade and Proxy patterns also don't convert the interface.

2) One of the major differences between Decorator and Proxy patterns is that Decorator never creates an object, it always adds new functionality on an already existing objects, on there other hand Proxy may create an object if it's not existed. It can stand in place of a real object, until it's ready and then start forwarding requests to it.

3) Decorator design Pattern also allows to add multiple features and can do it in an ordered fashion, by chaining multiple decorators, while proxy pattern doesn't advise chaining of proxies.

4) If we compare Decorator with Facade pattern, then you can see that unlike Decorator, facade doesn't add any new behavior, it just call existing methods from interface, it provide as a facade.

5) Unlike Decorator, Proxy, and Adapter design patterns, it's not required for a Facade to implement any particular interface. In fact, Facade can even be a class, just holding individual sub system components and providing simpler operations required by the client, and then invoking corresponding methods on the subsystem. 

For example, we can consider a Car as a facade, which provides start() and stop() method for starting and stopping. When client call start() method, it may be starting individual sub systems by calling there respective methods e.g. engine.start()wheels.move()lights.on()ac.on() etc.

UML diagram of Adapter, Decorator, Proxy and Facade Pattern

The similarities we have been talking about is much more visible in their structure. If you look at their UML diagrams you can clearly see the resemblance.

UML diagram of the Adapter pattern

Adapter pattern in Java

UML diagram of the Decorator pattern

Decorator design pattern

UML diagram of Proxy pattern

Proxy design pattern in Java

UML diagram of the Facade pattern

Facade design pattern in Java

That's all on the difference between Adapter, Decorator, Proxy, and Facade Design Patterns in Java. Understanding similarities and differences between them will improve your knowledge and ability to spot the use of design patterns. In short, use an Adapter design pattern if you need to convert interfaces, to make two parties work together.

You should use a Proxy design Pattern in Java if you need to hide real objects because of various reasons e.g. security, performance, networking, etc. Use Decorator pattern to add new behaviors on existing objects at runtime, it provides flexibility to mix behaviors and apply them in a different order as per clients requirements.

Finally use a Facade pattern to provide simplified access of a complex system to the client. Facade provides a higher level of abstraction and should contain operations required by the client.

Other Java Design Patterns tutorials you may like
  • 5 Free Courses to learn Object Oriented Programming (courses)
  • Difference between Factory and AbstractFactory Pattern? (example)
  • 18 Java Design Pattern Interview Questions with Answers (list)
  • How to design a Vending Machine in Java? (questions)
  • 20 System Design Interview Questions (list)
  • 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)
  • 7 Best Books to learn the Design Pattern in Java? (books)
  • How to implement the Strategy Design Pattern in Java? (example)
  • 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 Java design pattern tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.


Anonymous said...

design patterns are language-agnostic and can be implemented in any language. They are also not very same, you can see their class diagram are very different than each other. Facade may be treated as Adapter D.P. generalization - Adapter adopts 2 interfaces, and Facade is a new interface over 2 or more interfaces. If Facade needs to have complex logic to be used in communication between 2 or more contained interfaces Mediator will be helpful to encapsulate this logic

Unknown said...

Great article, but composition in decorator diagram is the wrong way. The component doesn't know anything about the decorator.

Dawid W. said...

Also decorator has a component and also implements its interface itself
public class Decorator : Component
private Component component;

public Decorator(Component component)
this.component = component;

/*Whatever methods interfaces has*/

Decorator can be passed same as decorated component, so any client of this code is agnostic as to if this is real or decorated component, because it uses same interface.

Post a Comment