What is the exact difference between Adapter and Proxy patterns?
Asked Answered
A

3

33

As I understood both Adapter and Proxy patterns make two distinct/different classes/objects compatible with each for communication. And both of them are Structural patterns. I am getting that both of them are pretty much similar with each other.

Can some one explain what exactly make(s) them different?

EDIT: I went through this question. But I'd rather like to have a close comparison between Adapter and Proxy.

Amaris answered 8/6, 2016 at 3:38 Comment(2)
Possible duplicate of How do the Proxy, Decorator, Adapter, and Bridge Patterns differ?Tyndall
@Tyndall I had gone through that, but I'd rather need a close comparison between these 2. Thnx anyway.Amaris
L
12

From here:

Adapter provides a different interface to its subject. Proxy provides the same interface.

You might think of an Adapter as something that should make one thing fit to another that is incompatible if connected directly. When you travel abroad, for example, and need an electrical outlet adapter.

Now a Proxy is an object of the same interface, and possibly the same base class (or a subclass). It only "pretends" to be (and behaves like) the actual object, but instead forwards the actual behavior (calculations, processing, data access, etc.) to an underlying, referenced object.

Extrapolating to the electrical analogy, it would be OK that the use of an adapter is visible to the client - that is, the client "knows" an adapter is being used - while the use of a proxy might more often be hidden, or "transparent" - the client thinks an actual object is being used, but it is only a proxy.

Laylalayman answered 8/6, 2016 at 3:57 Comment(0)
T
3

In practice the concepts wrapper, adapter and proxy are so closely related that the terms are used interchangeably.

  • As the name suggests, a wrapper is literally something that wraps around another object or function. e.g. a function that calls another function, or an object that manages the lifecycle of another object and forwards requests and responses.

  • An adapter literally adapts the contract. That commonly refers to changing the interface of an object, or changing a method signature. And in both cases that can only be accomplished by wrapping it with a different object or function.

  • The word proxy is used for exactly the same thing. However, some sources will use it more explicitly to refer to an adapter to access a remote resource. Basically, that means that local calls will be forwarded to a remote object. And it may appear natural to define a common interface which can then be shared/reused both locally and remotely for those objects.

Note: The latter interpretation of the proxy pattern isn't really a thing any more. This approach made sense in a time where technologies like CORBA were hot. If you have a remote service to access, it makes more sense to clearly define Request, Response and Context objects, and reach for technologies like OpenAPI or XSD.

Teat answered 29/4, 2022 at 20:21 Comment(0)
M
1

Difference between Adapter pattern and Proxy Pattern

ADAPTER PATTERN

  1. Indian mobile charger (CLIENT) does not fit in USA switch board (SERVER).
  2. You need to use adapter so that Indian mobile charger (CLIENT) can fit in USA switch board (SERVER).
  3. From point 2, you can understand that the CLIENT contacts adapter directly. Then adapter contacts server.

PROXY PATTERN

  • In adapter pattern client directly contacts adapter. It does not contact server.
  • In proxy pattern, proxy and server implements the same interface. Client would call the same interface.

UNDERSTANDING THROUGH CODE

class client{
    public void main(){
      //proxy pattern
      IServer iserver = new proxy();
      iserver.invoke();

      //adapter pattern
      IAdapter iadapter = new adapter();
      iserver.iadapter();
    }
}

class server implements IServer{
    public void invoke(){}
}

class proxy implments IServer{
  public void invoke(){}
}

class adapter implements IAdapter{
  public void invoke(){}
}

Reference: Difference between Adapter pattern and Proxy Pattern

Milliliter answered 25/1, 2018 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.