Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Proxy pattern and Adapter pattern?

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

like image 730
odiseh Avatar asked Sep 22 '10 06:09

odiseh


People also ask

What are 2 differences between proxy pattern and facade pattern?

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.

Is there a difference between Adaptor and facade pattern?

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.

What is the difference between Decorator and proxy pattern?

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.

What is the difference between proxy and wrapper?

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).


2 Answers

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.

like image 154
stombeur Avatar answered Oct 06 '22 17:10

stombeur


Proxies are typically used for the following scenarios:

  • The underlying 'real' object is expensive to create. You then have the proxy be a placeholder while it is created (a progressbar icon while downloading a huge image is a typical example). Lazy-loading is another typical example. The idea being that we don't know if the User will ever click the 'Details' pane - so we'll defer loading till he actually clicks it or the system is idle.
  • You want to control access (SecurityProxy) to some or all members of the 'real' object within the proxy.

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!

like image 45
Goblin Avatar answered Oct 06 '22 17:10

Goblin