It seems that there are similarities between Proxy and Adapter pattern?
Can any one please explain what are the differences? Why we require both of them? In which problems we should use only proxy and NOT another by an .net example?
Thank you
Key Points of Proxy vs FacadeThe purpose of the Proxy is to add behavior while The purpose of the Facade is to simplify, which may actually involve removing behavior. Proxy object represents a singly object while Facade object represents a subsystem of object.
The Adapter pattern allows the interface of an existing class to be used as another interface. The Façade pattern enables an object to provide a simplified interface to a larger body of code, such as a class library.
A Decorator requires an instance of the interface it is wrapping, while a Proxy does not require such an instance. A Proxy can receive an instance, but is also allowed to create this instance itself. So you can create a new Proxy on its own, while a Decorator needs another instance as dependency.
The wrapper pattern (aka the adapter pattern) takes one interface and adapts it to the other. A proxy implements an interface for the purpose of providing access to something else (usually something big).
A proxy exposes the exact same behavior as the object it hides. A proxy is typically used to contact a remote object without having to know how to contact it. An example is a WCF service, you can encapsulate accessing the service in a proxy that exposes the exact same interface as the wcf service, but hides the implementation details away like using a channelfactory and handling faultexceptions etc... It's like you client is talking to the WCF service locally.
An adapter also hides an underlying object, but it transforms the data you exchange with it to the right format and content used by the underlying object. An example is indeed a legacy system, like Goblin says. You encapsulate the complexity of talking to the legacy system (maybe it uses a chatty or CRUDy API and you want to hide it behind a coarse-grained operation) into an adapter to prvide a simple way of talking to the legacy system to your clients.
That's how I understand it at least.
EDIT: by the way, I personally feel that you don't have to see design pattern names as the end-all-do-all. Choose the right pattern based on what you want to achieve and call it whatever you want.
Proxies are typically used for the following scenarios:
Adaptors play another role - they bridge the gap between two classes that have no relation. The Adaptor can act as both objects. This is used primarily when one has to integrate with legacy systems (or 3rd party frameworks for that matter) where it is not possible to alter the API.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With