Thursday, March 12, 2015

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 simila

Read more: http://javarevisited.blogspot.com/search/label/design%20patterns#ixzz3UA7Vhx00

No comments:

Post a Comment