调解器与观察者面向对象的设计模式[英] Mediator Vs Observer Object-Oriented Design Patterns

本文是小编为大家收集整理的关于调解器与观察者面向对象的设计模式的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我一直在阅读 fout fout 的帮派解决我的一些问题并遇到介体模式.

我早些时候曾在项目中使用观察者来进行一些GUI应用.我有点困惑,因为两者之间没有发现很大的区别.我浏览以找到差异,但找不到我查询的任何合适答案.

有些人可以通过一些很好的例子来帮助我区分两者,这显然可以划分两者?

推荐答案

观察者模式: 定义对象之间的一对多依赖关系,以便当一个对象更改状态时,所有依赖者都会自动通知和更新.

调解器模式: 定义一个封装一组对象交互的对象.调解器通过防止对象互相引用,从而促进松散的耦合,并使您可以独立地改变它们的相互作用.

来源: dofactory

示例:

观察者模式: A类可以具有注册的O类型的零或更多观察者.当A中的某些东西更改时,它会通知所有观察者.

调解器模式: 您有一些X类的实例(或甚至几种不同类型:X,Y&Z),他们希望相互交流(但是您不希望每个人都有明确的参考),所以您创建一个中介类别.X的每个实例都有一个对M共享实例的引用,它可以通过该实例与X(或X,Y和Z)的其他实例进行通信.

其他推荐答案

在原始书中创造了术语观察者和中介,设计模式,可重复使用的对象的软件的元素,它说可以通过使用观察者模式来实现中介图案.但是,它也可以通过拥有同事(大致等于观察者模式的主题)来实现.

在很多情况下,您要使用观察者模式,它们的关键是,一个对象不知道其他对象正在观察其状态.

调解器更具体,它避免了直接交流课程,而是通过调解员进行交流.这通过允许沟通被卸载到处理沟通的课程来帮助单一责任原则.

一个经典的调解人示例是在GUI中,幼稚的方法可能会在按钮点击事件上导致代码,说"如果禁用了Foo面板,并且酒吧面板的标签上标有"请输入日期",则不要致电服务器,否则继续",其中有调解器模式可以说:"我只是一个按钮,对foo面板和酒吧面板上的标签一无所知,所以我只问我的调解人是否是否现在致电服务器是O.K.."

或,如果使用观察者模式实现调解器,则按钮会说:"嘿,观察者(包括调解人),我的状态更改了(有人单击我).如果您在乎,请做些事情".在我的示例中,可能比直接引用调解人的示例不太有意义,但是在许多情况下,使用观察者模式实现调解人是有道理的,并且观察者和中介者之间的差异比代码本身的差异更重要.

其他推荐答案

观察者

1.没有

  • client1 :嘿主题,您什么时候更改?

  • 客户端2 :您什么时候更改主题?我没有注意到!

  • client3 :我知道主题已更改.

2.用

  • 客户保持沉默.
  • 一段时间后...
  • 主题:亲爱的客户,我改变了!

中介

1.没有

  • client1 :嘿 taxi1 ,带我一些地方.
  • client2 :嘿 taxi1 ,带我一些地方.
  • client1 :嘿 taxi2 ,带我一些地方.
  • client2 :嘿 taxi2 ,带我一些地方.

2.用

  • 客户端1 :嘿出租车,请带我出租车.
  • 客户端2 :嘿出租车,请带我出租车.

本文地址:https://www.itbaoku.cn/post/627662.html

问题描述

I have been reading the Gang Of Four, in order to solve some of my problems and came across the Mediator pattern.

I had earlier used Observer in my projects for making some GUI application. I am a bit confused as I do not find great difference between the two. I browsed to find the difference but could not find any apt answer for my query.

Could some one help me to differentiate between the two with some good example which clearly demarcates the two?

推荐答案

The Observer pattern: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

The Mediator pattern: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Source: dofactory

Example:

The observer pattern: Class A, can have zero or more observers of type O registered with it. When something in A is changed it notifies all of the observers.

The mediator pattern: You have some number of instances of class X (or maybe even several different types:X, Y & Z), and they wish to communicate with each other (but you don't want each to have explicit references to each other), so you create a mediator class M. Each instance of X has a reference to a shared instance of M, through which it can communicate with the other instances of X (or X, Y and Z).

其他推荐答案

In the original book that coined the terms Observer and Mediator, Design Patterns, Elements of Reusable Object-Oriented Software, it says that the Mediator pattern can be implemented by using the observer pattern. However it can also be implemented by having Colleagues (which are roughly equivalent to the Subjects of the Observer pattern) have a reference to either a Mediator class or a Mediator interface.

There are many cases when you would want to use the observer pattern, they key is that an object should not know what other objects are observing it's state.

Mediator is a little more specific, it avoids having classes communicate directly but instead through a mediator. This helps the Single Responsibility principle by allowing communication to be offloaded to a class that just handles communication.

A classic Mediator example is in a GUI, where the naive approach might lead to code on a button click event saying "if the Foo panel is disabled and Bar panel has a label saying "Please enter date" then don't call the server, otherwise go ahead", where with the Mediator pattern it could say "I'm just a button and have no earthly business knowing about the Foo panel and the label on the Bar panel, so I'll just ask my mediator if calling the server is O.K. right now."

Or, if Mediator is implemented using the Observer pattern the button would say "Hey, observers (which would include the mediator), my state changed (someone clicked me). Do something about it if you care". In my example that probably makes less sense then directly referencing the mediator, but in many cases using the Observer pattern to implement Mediator would make sense, and the difference between Observer and Mediator would be more one of intent than a difference in the code itself.

其他推荐答案

Observer

1. Without

  • Client1: Hey Subject, when do you change?

  • Client2: When did you change Subject? I have not noticed!

  • Client3: I know that Subject has changed.

2. With

  • Clients are silent.
  • Some time later ...
  • Subject: Dear clients, I have changed!

Mediator

1. Without

  • Client1: Hey Taxi1, take me some where.
  • Client2: Hey Taxi1, take me some where.
  • Client1: Hey Taxi2, take me some where.
  • Client2: Hey Taxi2, take me some where.

2. With

  • Client1: Hey TaxiCenter, please take me a Taxi.
  • Client2: Hey TaxiCenter, please take me a Taxi.