Top Banner

of 27

Postman Pattern

Nov 05, 2015

Download

Documents

JiahaoLiuLiu

Postman Pattern is a improved Observer pattern specially for mobile phones. It deals with the problem that from the mobile phone, the observer could be not in the foreground when the data is coming from observable.

For this case, the observable acts like the postman, leaving the data (package) in the observer. When the Observer became visible again, it will check if it has some data (package) to be processed. If so, the data will be processed.

It includes the implementation for Android and some highlight for the source code, which is stored in GitHub here:
https://github.com/jiahaoliuliu/PostmanPattern/tree/simplePostmanPattern
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • Postman PatternAdvance Observer pattern for Mobile

    by Jiahao Liu

  • Postman PatternAdvance Observer pattern for Mobile

  • Index

    The problem

    Postman pattern

    Further improvement

  • The Problem

  • Observer pattern

  • Observer

    Ask observable about data

    Notified when the data is available

    Can unregister itself

  • Observable

    Register observer

    Contains a list of observer

    Notify observers when data is ready

  • Problem with mobile

    UI screen as observer

    Observable could return data in any moment

    UI screen could not be in foreground

    It cannot modify the view of another UI screen

  • Postman pattern

  • Real world example

    Postman

  • RequestImagine you have have a shop and

    you request a package

  • PreparationThe sender takes some time to

    prepare the package and send it to the post oce

  • ReceptionWhen the postman delivers the

    package to your shop, the shop could be open or close

  • Shop openDirect deliver

  • Shop closedJust leave the package in the mail

    box.

    When the shop opens again, the package will be checked

  • Sample codeSample project on Android

  • Postman Activity

    Abstract class

    Implements Observer

    protected boolean isInForeground

    Updated by onResume() and onPause()

  • Postman Activity

    protected abstract void processDataIfExists()

    Called onResume()

    1. If the data does not exists, finish

    2. Otherwise, process the data

    3. And remove the data

  • Activities

    Extends from PostmanActivity

    Implements processDataIfExists()

    Implements update(Observable, Object)

    If (isInForeground) processDataIfExists()

    observable.deleteObserver(this)

  • Postman Observable

    Extends from Observable

    Add Observer

    Get data

    Notify observer

    delete observer

  • User case

  • User case

    MainActivity extends from PostmanActivity

    1. MainActivity request data to PostmanObservable

    2. PostmanObservable register the observer

    3. PostmanObservable prepare the data and notify to MainActivity

  • User case

    Update from MainActivity is called

    1. Save the data

    2. If MainActivity is in foreground

    Invoke processDataIfExist()

  • User caseUpdate from MainActivity is called

    3. If MainActivity is not in foreground

    Do nothing

    When the MainActivity goes on foreground

    Invoke processDataIfExist()

    4. Delete itself from the observable

  • Sample code herehttps://github.com/jiahaoliuliu/

    PostmanPattern/tree/simplePostmanPattern

  • Further improvement

  • Multiple data needed

    If multiple data is needed from the same observable, it is not possible.

    The observer is removed from observable after first result