Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Journey to understand Observer and Pub-Sub Pattern

Journey to understand Observer and Pub-Sub Pattern

Amanjeet Singh

October 20, 2018
Tweet

More Decks by Amanjeet Singh

Other Decks in Programming

Transcript

  1. Journey to understand Observer and Pub-Sub Pattern

  2. Let’s understand the need? • Derivation in a mathematical way

  3. • Derive: What is observer pattern and pub sub pattern

    and why do we need them. • Given: iPhone, User, Different situations • Assumption: Let’s assume that there are no observers or pub sub patterns. What will happen: 1. No real time modifying view controllers:
  4. 2. Periodic Monitoring increases • Regular monitoring in regular intervals

    • Periodic networking calls • Periodic monitoring of different resources.
  5. 3. Checking the state of resources periodically

  6. • Result: Nobody wants this and thus we can say

    we need someone to tell about the change.
  7. Observer pattern & Publisher-Subscriber Pattern

  8. Observer Pattern “The observer pattern is a software design pattern

    in which an object, called the subject maintains a list of its dependents, called observers, and notifies automatically of any state changes, usually by calling one of their methods.” -Wikipedia
  9. Pub Sub Pattern • Pub: Publishers • Sub: Subscribers •

    A software design pattern where there are senders of some messages and they send the messages to the receivers called the subscribers. -Unknown
  10. Confusing??

  11. Difference in implementation Observer Pattern

  12. Pub-Sub Pattern Event Channel in between called an Event Bus

    or Broker Pub Sub Pattern
  13. APIs providing such facilities • Notification Center • Observation Protocols

    • RxSwift • Grand Central Dispatch • MQTT • Delegates
  14. Notification Centers Posting Notifications

  15. Receiving the Notification

  16. Receiving the Notification

  17. Receiving the Notification

  18. Receiving the Notification

  19. Using Protocols

  20. Event Bus using RxSwift • RxSwift • Understanding of Reactive

    Programming • Publish Subjects
  21. Publish Subjects • Subjects: Observable + Observer • It emits

    all the subsequent items of the source Observable at the time of subscription.
  22. Use case • Event Bus to broadcast the subscribers about

    the events of your journey. • Beginning of journey • Ending of Journey • Journey updates • Any journey related channel
  23. Steps to success!! • Create some dummy locations • Create

    a singleton class to set the Journey Events and get the journey events
  24. Steps to success!! • Create some dummy locations • Create

    a singleton class to set the Journey Events and get the journey events
  25. Steps to success!! • Create some dummy locations • Create

    a singleton class to set the Journey Events and get the journey events
  26. Steps to success!! • Create some dummy locations • Create

    a singleton class to set the Journey Events and get the journey events
  27. • Subscribe to the “Resource” in viewDidLoad( ) • Now

    you can get the events wherever you write this subscribe code
  28. • Subscribe to the “Resource” in viewDidLoad( ) • Now

    you can get the events wherever you write this subscribe code
  29. • Subscribe to the “Resource” in viewDidLoad( ) • Now

    you can get the events wherever you write this subscribe code
  30. Setting the events

  31. Setting the events

  32. Thank You # @droid_singh amanjeetsingh150