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

    View Slide

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

    View Slide

  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:

    View Slide

  4. 2. Periodic Monitoring increases
    • Regular monitoring in regular intervals
    • Periodic networking calls
    • Periodic monitoring of different resources.

    View Slide

  5. 3. Checking the state of resources periodically

    View Slide

  6. • Result: Nobody wants this and thus we can say we need
    someone to tell about the change.

    View Slide

  7. Observer pattern
    &
    Publisher-Subscriber Pattern

    View Slide

  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

    View Slide

  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

    View Slide

  10. Confusing??

    View Slide

  11. Difference in implementation
    Observer Pattern

    View Slide

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

    View Slide

  13. APIs providing such facilities
    • Notification Center

    • Observation Protocols

    • RxSwift

    • Grand Central Dispatch

    • MQTT

    • Delegates

    View Slide

  14. Notification Centers
    Posting Notifications

    View Slide

  15. Receiving the Notification

    View Slide

  16. Receiving the Notification

    View Slide

  17. Receiving the Notification

    View Slide

  18. Receiving the Notification

    View Slide

  19. Using Protocols

    View Slide

  20. Event Bus using RxSwift
    • RxSwift

    • Understanding of Reactive Programming

    • Publish Subjects

    View Slide

  21. Publish Subjects
    • Subjects: Observable + Observer
    • It emits all the subsequent items of the source
    Observable at the time of subscription.

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  30. Setting the events

    View Slide

  31. Setting the events

    View Slide

  32. Thank You #
    @droid_singh amanjeetsingh150

    View Slide