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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

  5. 3. Checking the state of resources periodically

    View full-size slide

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

    View full-size slide

  7. Observer pattern
    &
    Publisher-Subscriber Pattern

    View full-size 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 full-size 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 full-size slide

  10. Difference in implementation
    Observer Pattern

    View full-size slide

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

    View full-size slide

  12. APIs providing such facilities
    • Notification Center

    • Observation Protocols

    • RxSwift

    • Grand Central Dispatch

    • MQTT

    • Delegates

    View full-size slide

  13. Notification Centers
    Posting Notifications

    View full-size slide

  14. Receiving the Notification

    View full-size slide

  15. Receiving the Notification

    View full-size slide

  16. Receiving the Notification

    View full-size slide

  17. Receiving the Notification

    View full-size slide

  18. Using Protocols

    View full-size slide

  19. Event Bus using RxSwift
    • RxSwift

    • Understanding of Reactive Programming

    • Publish Subjects

    View full-size slide

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

    View full-size slide

  21. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  29. Setting the events

    View full-size slide

  30. Setting the events

    View full-size slide

  31. Thank You #
    @droid_singh amanjeetsingh150

    View full-size slide