Sunday, January 1, 2017

Adapter vs Decorator vs Facade vs Proxy Design Pattern in Java

There is some striking similarity between Adapter, Decorator, Facade and Proxy design pattern, in the sense that they all use Composition and delegation to solve the problem. Adapter pattern wraps an interface, and delegates call to it. Decorator wraps an object and implement behaviour on top of that, Facade wraps one or more interface to provide a central interface, which is easy to use and Proxy Pattern also wraps Subject and delegates calls to it. Then questions comes, why they are different patterns? What is difference between Adapter, Decorator, Facade or Proxy pattern, if there structure is same. Answer is Intent. Yes, all of these Java design pattern has similar structure and class diagrams but there intents are totally different to each other. Main purpose of Adapter pattern is to convert interfaces. Adapter let's two components working together, which wouldn't be working because of incompatible interfaces. Decorator pattern, adds new functionalities at runtime. It allows you to enrich object, even after it's creation. Facade design pattern neither converts interfaces nor adds new functionality, instead it just provide simpler interfaces. So instead of client directly accessing individual components of a system, it uses facade. Facade design pattern allows client to interact with complex system with much simpler interface and less work. Facade will then call individual components. Proxy pattern is also quite similar to Adapter and Decorator, but it's purpose is to control access of object. Proxy prevents client's to directly accessing object, instead it act as real object and can provide alternate behavior or forward request to original object. Proxy is most versatile pattern of all these, and it can be used in different ways e.g. remote proxy for communicating with remote object, virtual proxy to control access of expensive object, protection proxy to provide access to object 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.

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 are meant to stay in place of original object, that's why both Decorator and Proxy pattern implements interface of real object. Because of this nature, a decorator and proxy can be passed to a method, which accepts original or real object.

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

5) Another similarity between Adapter and Facade pattern is that they can wrap multiple interfaces, which is also a different than Decorator and Proxy pattern, 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 lot of similarities between all of these structural patterns, let's see some difference between them. As I have said earlier, they differ in there 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 original object. Facade and Proxy patterns also don't convert interface.

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

3) Decorator design Pattern also allows to add multiple features and can do it in a 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 pattern, it's not require 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 client, and then invoking corresponding methods on 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 clear see the resemblance.

UML diagram of Adapter pattern
Adapter pattern in Java

UML diagram of Decorator pattern
Decorator design pattern

UML diagram of Proxy pattern
Proxy design pattern in Java

UML diagram of Facade pattern
Facade design pattern in Java

That's all on 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 use of design patterns. In short, use Adapter design pattern if you need to convert interfaces, to make two parties work together. Use Proxy design Pattern in Java, if you need to hide real object because of various reason e.g. security, performance, networking etc. Use Decorator pattern to add new behaviors on existing object at runtime, it provides flexibility to mix behaviors and apply them in different order as per clients requirement. Finally use Facade pattern to provide simplified access of a complex system to client. Facade provides higher level of abstraction, and should contain operations required by client.

Further Learning
Design Pattern Library
From 0 to 1: Design Patterns - 24 That Matter - In Java
Java Design Patterns - The Complete Masterclass


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

Stanley F. said...

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

Post a Comment