推拉机制观察者模式 [英] Push, pull mechanism observer pattern

查看:206
本文介绍了推拉机制观察者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在研究设计模式,而进入某个部分时,我感到困惑的是观察者模式是利用推入机制还是利用推入机制?

At the moment I'm studying design patterns and I've come to a part where I'm confused whether the observer pattern makes use of the push mechanism or does it make use of the pull mechanism?

我已经阅读了此方法的不同实现,但无法真正确定哪个是正确的。

I've read different implementations of this and can't really establish which one is correct.

此外,我想了解推模型对拉模型的三个直接优势。
我想其中之一是推模型与拉模型之间的耦合程度较低?

Also, I'd like to know three straight forward advantages of the push model towards the pull model. I guess one of them is that the push model is less coupled then the pull model?

推荐答案

详细信息(重点放在提出的问题上)







  • 定义:观察者模式定义了对象之间的一对多依赖关系,因此当一个对象改变状态时,其所有依赖关系都会得到通知并自动更新

    Observer Pattern in detail (with the focus on questions asked)


    • Definition : The Observer Pattern defines a one-to-many dependency between the objects so that when one object changes state, all of its dependents are notified and updated automatically

      要重点关注的三件事


      1. 可观察对象-该对象是

      2. 观察者对象-观察可观察对象的对象

      3. 通信机制 -推拉机制

      1. Observable object - The object being observed.
      2. Observer objects - The objects that observe the observable object
      3. Communication Mechanism - Pull or Push Mechanism



    • 目前我正在研究设计模式,而来到某个地方,我感到困惑的是观察者模式是利用推动机制还是利用拉动机制nism吗?

      At the moment i'm studying design patterns and i've came to a part where i'm confused whether the observer pattern makes use of the push mechanism or does it makes use of the pull mechanism ?

      混乱可能是因为您是文学名流-拉或推。

      Confusion might be because you are literary going by name - Pull or Push.

      请注意,在这两种机制中,始终将Observable对象通知所有已订阅的观察者,但区别在于[Push->]观察者是否获得所需的确切数据或[Pull-> ]它获取包装在某个对象(主要是可观察对象)中的数据。

      Please note that in both mechanisms, it is always the responsibility of Observable object to notify all the subscribed observers, but the difference lies whether [Push->]the observer get the exact data it wants or [Pull->] it get the data wrapped in some object (mostly Observable object) & it has to extract the required data from it.


      • Push-> Observer直接获取所需数据

      • Pull-> Observer获取包装在对象中的数据,它需要提取该数据。


      我已经阅读了此方法的不同实现,但无法真正确定哪个是正确的。

      I've read different implementations of this and can't really establish which one is correct.

      这不是更正的问题,实际上两者在任何情况下都可以正常工作。正是这种方式最适合特定的场景/情况,如果我们看到以下两种机制的详细信息,就可以轻松地进行分析。

      It is not the question of correction here, actually both will work absolutely fine in any situation. It's just which is best suited to a particular scenario/situation which can be easily analysed if we see following details for both the mechanism.


      我想知道推模型对拉模型的三个直接优点。我想其中之一是推模型与拉模型的耦合性更低?

      Also i'd like to know three straight forward advantages of the push model towards the pull model. I guess one of them is that the push model is less coupled then the pull model ?

      我可能无法提供3个优势,但让我尝试,如果我可以通过用例示例清楚地说明在哪里使用内容:

      I may not be able to provide 3 advantages, but let me try if I can give you a clear picture of where to use what by using use-case examples:


      • 推送机制

      • Push Mechanism


      • 这纯粹是Observable的职责,Observer只需确保已将所需的代码放入其更新方法中即可。

      • 优点


        • 推模型的主要优点是观察者与主题之间的耦合性较低。


          • 可观察的&观察者都是接口/抽象类,实际上是设计原则-接口或超类型的程序

          • This is purely the Observable's responsibility, Observer just need to make sure they have put required code in their update methods.
          • Advantages
            • The main advantage of the 'push' model is lower coupling between the observer and the subject.
              • Observable & Observer both are interfaces/abstract classes which is actually a design principle - Program to interface or supertypes

              • 灵活性不足:由于Observable需要将所需数据发送给Observer,如果我们说有1000个观察者,其中大多数需要不同类型的数据。


              • 当最多有2-3种不同类型的观察者(不同类型的观察者需要不同的数据)或所有观察者需要相同类型的数据时,应使用此

              • 像银行中的令牌系统


                • 在这种情况下,所有观察者(不同的LED)仅需要一个通知,即可获取更新的等待令牌编号列表,因此可能更好
                • It should be used when there are max 2-3 different types of Observers (different types means observer require different data) or all observers require same type of data.
                • Like token systems in a bank
                  • In this all observers (different LEDs) just need one notification the list of updated waiting token numbers, so may better be implemented in this way as compared to Pull Mechanism.

                  拉动机制


                  • 在拉取机制中,Observable的责任是通知所有观察者某件事已更改,但是这次Observable共享具有更改的整个对象,某些观察者可能不需要完整的对象,因此观察者只需提取

                  • 优点


                    • 这样做的好处是灵活性更高。


                      • 每个观察者都可以自行决定要查询的内容,而不必依赖主题发送正确的(仅必需的)信息。


                      • 观察者会为了从共享的完整对象中查询正确的信息,必须了解有关主题的信息。


                      • 当存在2-3种以上不同类型的观察者(不同类型的观察者需要不同的数据)时,应使用该 就像任何外汇汇率提供商为不同的投资银行发布外汇汇率一样


                        • 在这种情况下,有些银行仅处理INR,另一些仅处理GBP等。

                        参考文献

                        • Head-First book for design Patterns
                        • https://softwareengineering.stackexchange.com/questions/253398/the-observer-pattern-using-the-pulling-mechanism?newreg=999c28a6a1f6499783fbe56eb97fa8ec
                        • https://dzone.com/articles/observer-pattern

                        这篇关于推拉机制观察者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆