What is the exact difference between Adapter and Proxy patterns?

旧街凉风 提交于 2019-12-01 03:14:45
Ravindra babu

Adapter:

  1. It allows two unrelated interfaces to work together through the different objects, possibly playing same role.
  2. It modifies original interface.

UML diagram:

You can find more details about this pattern with working code example in this SE post:

Difference between Bridge pattern and Adapter pattern

Proxy:

Proxy provide a surrogate or place holder for another object to control access to it.

UML diagram:

There are common situations in which the Proxy pattern is applicable.

  1. A virtual proxy is a place holder for "expensive to create" objects. The real object is only created when a client first requests/accesses the object.
  2. A remote proxy provides a local representative for an object that resides in a different address space. This is what the "stub" code in RPC and CORBA provides.
  3. A protective proxy controls access to a sensitive master object. The "surrogate" object checks that the caller has the access permissions required prior to forwarding the request.
  4. A smart Proxy provides sophisticated access to certain objects such as tracking the number of references to an object and denying access if a certain number is reached, as well as loading an object from database into memory on demand

For working code, have a look at tutorialspoint article on Proxy.

Key differences:

  1. Adapter provides a different interface to its subject. Proxy provides the same interface
  2. Adapter is meant to change the interface of an existing object

You can find more details about these patterns in sourcemaking articles of proxy and adapter articles.

Other useful articles: proxy by dzone

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!