简体   繁体   English

适配器和代理模式之间的确切区别是什么?

[英]What is the exact difference between Adapter and Proxy patterns?

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.但我更愿意对 Adapter 和 Proxy 进行仔细比较。

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: UML图:

在此输入图像描述

You can find more details about this pattern with working code example in this SE post: 您可以通过此SE帖子中的工作代码示例找到有关此模式的更多详细信息:

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: UML图:

在此输入图像描述

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. 这就是RPC和CORBA中提供的“存根”代码。
  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. 有关工作代码,请查看有关Proxy的tutorialspoint文章。

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. 您可以在代理适配器文章的sourcemaking文章中找到有关这些模式的更多详细信息。

Other useful articles: proxy by dzone 其他有用的文章: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). 现在,Proxy是同一接口的对象,可能是相同的基类(或子类)。 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. 根据电气类比推断,客户端可以看到适配器的使用 - 也就是说,客户端“知道”正在使用适配器 - 而代理的使用可能更常被隐藏,或者“透明“ - 客户端认为正在使用实际对象,但它只是一个代理。

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. eg a function that calls another function, or an object that manages the lifecycle of another object and forwards requests and responses.顾名思义,包装器实际上是围绕另一个 object 或 function 的东西。例如,调用另一个 function 的 function,或管理另一个 object 的生命周期并转发请求和转发响应的 object。

  • An adapter literally adapts the contract.适配器从字面上适应合同。 That commonly refers to changing the interface of an object, or changing a method signature.这通常是指更改 object 的接口,或更改方法签名。 And in both cases that can only be accomplished by wrapping it with a different object or function.在这两种情况下,这只能通过用不同的 object 或 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 .基本上,这意味着本地呼叫将被转发到远程 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.这种方法在CORBA等技术炙手可热的时代是有意义的。 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.如果您要访问远程服务,那么明确定义请求、响应和上下文对象并使用 OpenAPI 或 XSD 等技术会更有意义。

Difference between Adapter pattern and Proxy Pattern 适配器模式和代理模式之间的区别

ADAPTER PATTERN 适配器模式

  1. Indian mobile charger (CLIENT) does not fit in USA switch board (SERVER). 印度移动充电器(CLIENT)不适用于美国开关板(SERVER)。
  2. You need to use adapter so that Indian mobile charger (CLIENT) can fit in USA switch board (SERVER). 您需要使用适配器,以便印度移动充电器(CLIENT)可以放入美国开关板(SERVER)。
  3. From point 2, you can understand that the CLIENT contacts adapter directly. 从第2点开始,您可以了解CLIENT直接联系适配器。 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 参考: 适配器模式和代理模式之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'Adapter'和'Mediator'模式之间的确切区别是什么? - What is the exact difference between 'Adapter' and 'Mediator" patterns? 设计模式-适配器和桥接器之间的区别? - Design patterns - Difference between Adapter and Bridge? 继承和抽象类之间的确切区别是什么? - What is exact difference between Inheritance and Abstract class? 工厂函数和模块模式有什么区别? (更多下文) - What is the difference between factory functions and module patterns? (more below) 覆盖和隐藏之间的确切区别 - Exact difference between overriding and hiding 这两种方法有什么区别[Smalltalk Best Practice Patterns - Kent Beck]? - What is the difference between those two approaches [Smalltalk Best Practice Patterns - Kent Beck]? 具有 static 内部 class 的 Builder 模式与具有 One Abstract 和 one+ 具体实施的 Builder 设计模式有什么区别 - What is the difference between Builder Pattern with static inner class and Builder design patterns with One Abstract and one+ concrete implimentations else if 和 && 或 || 有什么区别? - What is the difference between else if and && or ||? PHP中的 - >和::有什么区别? - What is the difference between -> and :: in PHP? 复制和克隆有什么区别? - What is the difference between copying and cloning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM