[英]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: 适配器:
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. 存在代理模式适用的常见情况。
For working code, have a look at tutorialspoint article on Proxy. 有关工作代码,请查看有关Proxy的tutorialspoint文章。
Key differences: 主要差异:
You can find more details about these patterns in sourcemaking articles of proxy and adapter articles. 您可以在代理和适配器文章的sourcemaking文章中找到有关这些模式的更多详细信息。
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 适配器模式
PROXY PATTERN 代理模式
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.