Push, pull mechanism observer pattern

前端 未结 3 620
名媛妹妹
名媛妹妹 2020-12-29 16:12

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

3条回答
  •  庸人自扰
    2020-12-29 16:32

    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

    • 3 things to focus:

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

    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.

    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 get the required data directly
    • Pull -> Observer get the data wrapped in an object and it needs to extract that.

    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 ?

    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

      • 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
      • Disadvantage
        • Less flexibility : As Observable needs to send the required data to the Observer and it would become messy if we have say 1000 observers and most of them require different types of data.
      • Use-Case
        • 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.
    • Pull Mechanism

      • In pull mechanism as well, it is the Observable's responsibility to notify all observer that something has been changed, but this time Observable shares the whole object which has the changes, some observers might not require the complete object, so Observers just need extract the required details from that complete project in their update methods.
      • Advantages
        • The advantage of this is more flexibility.
          • Each observer can decide for itself what to query, without relying on the subject to send the correct (only required) information.
      • Disadvantage
        • The observers would have to know things about the subject in order to query the right information from the shared complete object.
      • Use-Case
        • It should be used when there are more than 2-3 different types of Observers (different types means observer require different data)
        • Like publishing of FX Rates by any foreign exchange rates provider for different investment banks
          • In this Some banks just deals with only INR, some others only GBP etc. so it should be implemented using Pull mechanism as compared to Push mechanism.

    References

    • 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

提交回复
热议问题