简体   繁体   English

'Adapter'和'Mediator'模式之间的确切区别是什么?

[英]What is the exact difference between 'Adapter' and 'Mediator" patterns?

I know that Adapter is a structural pattern and Mediator is a behavioral one. 我知道适配器是一种结构模式,而Mediator是一种行为模式。 But as far I understood, what both of them are doing, is connecting two (or more) other classes which are potentially incompatible (not so maintainable) for direct communication. 但据我所知,他们两人正在做的是连接两个(或更多)其他类,这些类可能与直接通信不兼容(不可维护)。

Can some one give a close comparison between these two and point out the exact difference? 有人可以对这两者进行比较,并指出确切的区别吗?

These are the links for Adapter and Mediator explanations in TutorialsPoint. 这些是TutorialsPoint中AdapterMediator解释的链接。

And these are sourcemaking explanations. 这些都是来源制作的解释。 Adapter , Mediator . 适配器调解器

They don't have much in common, IMO. 他们没有太多共同点,IMO。

A mediator is used to avoid coupling several components together. 介体用于避免将多个组件耦合在一起。 Instead of each component "talking" with each other directly (and thus having to know each other and to know how to communicate all with each other), each component talks to a single object: the mediator. 而不是每个组件直接相互“交谈”(因此必须彼此了解并知道如何彼此通信),每个组件与单个对象进行对话:中介。 The name is chosen on purpose: when you're fighting with your neighbor and can't communicate with him, you go see a mediator and instead of talking to each other, you both talk with the mediator, who tries fixing the issue. 这个名字是故意选择的:当你和邻居打架并且无法与他交流时,你会看到一个调解员,而不是互相交谈,你们都会与试图解决问题的调解员交谈。

An adapter is used to "transform" an object with an interface into an object with an other interface. 适配器用于将具有接口的对象“转换”为具有其他接口的对象。 Just like, for example, an electrical adapter which transforms a european power outlet into an american one, so that you can use your American shaver in Europe. 就像,例如,一个电源适配器,将欧洲电源插座转换为美国电源插座,这样您就可以在欧洲使用您的美国剃须刀。 Simple example: you need to store a Runnable into a list of Callables. 简单示例:您需要将Runnable存储到Callables列表中。 Runnable has a method run(). Runnable有一个方法run()。 Callable has a method call(). Callable有一个方法call()。 You thus create an Adapter: 因此,您创建一个适配器:

public class RunnableAdapter implements Callable {
    private Runnable runnable;

    public RunnableAdapter(Runnable runnable) {
        this.runnable = runnable;
    }

    public void call() {
        runnable.run();
    }
}

JB Nizet already wrote a good answer. JB Nizet已经写了一个很好的答案。 I just want to explain the differences in simpler words: 我只想用简单的词来解释差异:

  • Mediator should be used when you don't know how to communicate with other objects or you aren't allowed to 当您不知道如何与其他对象通信或者您不被允许时,应该使用Mediator

  • Adapter should be used when you know exactly how to communicate with objects, but these objects might not support some communication methods or differ 确切知道如何与对象通信时,应使用适配器 ,但这些对象可能不支持某些通信方法或不同

Adapter Pattern is useful when we already have two code base one consumer code and other producer code but the format in which Consumer wish the product to be in is different from what Producer code is producing. Adapter Pattern的时候,我们已经有两个代码库是一个有用的consumer code和其他producer code ,但在格式 Consumer希望产品是从什么不同Producer的代码生成。 Here as Producing code is already there in place and we do not wish to modify the existing code [ code closed for modification, open for extension ] . 这里因为生产代码已经到位,我们不希望修改现有代码[代码已关闭以进行修改,为扩展而打开] Adapter class can transform the product produced by Producer into format as expected by Consumer code. Adapter类可以将Producer生成的产品转换为Consumer代码所期望的格式。 Format may be the APIs whose return type is different as per what Producer code and expectation of the consumer code. 格式可以是API,其返回类型根据生产者代码和消费者代码的期望而不同。 Adapter class uses the API of the producer code and transforms them as per the expectation of the consumer. Adapter类使用生产者代码的API,并根据消费者的期望对其进行转换。 在此输入图像描述

Now Mediator pattern is useful when we are in process of designing the architecture or while refactoring. 现在,当我们正在设计架构或重构时,Mediator模式非常有用。 It helps in easy and loosely coupled interaction of objects. 它有助于轻松和松散地耦合对象的交互。 Define an object [Mediator] that encapsulates how a set of objects interact . 定义一个对象[Mediator],它封装了一组对象的交互方式 Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Mediator通过使对象明确地相互引用来促进松散耦合,并且它允许您独立地改变它们的交互。 在此输入图像描述

Can some one give a close comparison between these two and point out the exact difference? 有人可以对这两者进行比较,并指出确切的区别吗?

The intent and checklist in sourcemaking links, which have been quoted in your question provides good insight. 源代码制作链接中的意图清单已在您的问题中引用,可提供良好的洞察力。

Adapter convert the interface of a class into another interface clients expect 适配器将类的接口转换为客户期望的另一个接口

Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Mediator通过使对象明确地相互引用来促进松散耦合,并且它允许您独立地改变它们的交互。

isn't there something that implicitly means that mediator is an adapter that supports for than 2 classes. 是不是隐含地意味着mediator是一个支持2个以上类的适配器。 Due to this reason, Mediator can't act as an Adapter . 由于这个原因, Mediator不能充当适配器

  1. Mediator does not transform incompatible interface to compatible interface, what client expects unlike Adapter . Mediator不会将不兼容的接口转换为兼容接口,客户端期望与Adapter不同。

  2. Mediator interacts with Collegues of same interface. Mediator与相同接口的Collegues交互。

  3. Mediator abstracts/centralizes arbitrary communication between Colleague objects Mediator摘要/集中了Colleague对象之间的任意通信

Related posts with code examples: 相关帖子包含代码示例:

Mediator Vs Observer Object-Oriented Design Patterns Mediator Vs Observer面向对象的设计模式

Difference between Bridge pattern and Adapter pattern 桥模式和适配器模式之间的差异

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

相关问题 适配器和代理模式之间的确切区别是什么? - What is the exact difference between Adapter and Proxy 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? Mediator Vs Observer面向对象的设计模式 - Mediator Vs Observer Object-Oriented Design Patterns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM