Android MVP - Google Sample 解讀


寫在前頭: 一直想要碰 Android MVP 很久了,終於下定決心硬是抽出時間來練習一番,剛好一些範例也都成熟,教學文章也很多了,上手起來也比較快。以下是我一邊學習,一邊整理的筆記。建議是先把 code clone 下來,一邊對照著看 (我就是這樣一邊補筆記的lol 大大們還請鞭小力點)。

從 Android 官方的 googlesamples/android-architecture 中學習。其範例代碼仍在繼續更新中,如 TODO-MVP last updated at 2016.11.23。



Key concepts 這一段我認為非常重要。

在範例中,是先切了 4 個 feature:

  • Tasks
  • TaskDetail
  • AddEditTask
  • Statistics

每個 feature 再各自有:

  • A contract defining the view and the presenter
  • An Activity which is responsible for the creation of fragments and presenters
  • A Fragment which implements the view interface.
  • A presenter which implements the presenter interface

In general, the business logic lives in the presenter and relies on the view to do the Android UI work.

商業邏輯都該放在 presenter,至於 Android UI work 都倚賴 view 來做。

The view contains almost no logic: it converts the presenter's commands to UI actions and listens to user actions, which are passed to the presenter.

view 幾乎不該有其它邏輯。只是單純地

  1. 轉換 presenter 來的指令,變成 UI actions
  2. listen to user actions,然後傳給 presenter

Contracts are interfaces used to define the connection between views and presenters.

contract 是一個 interface ,把 view 跟 presenter 連結在一起。


  1. (如同範例) 先切 feature,再放 mvp
  2. 依照 mvp 直接開資料夾


接下來的東西就比較多 & 雜了,想要分到後續細講,迫不及待的朋友可以先看看下面的延伸閱讀。

下一篇:Android MVP - Google Sample 解讀 TaskDetail

延伸閱讀:約莫都是 2015.11 ~ 2016.6 的文章。

更廣泛地介紹 Android MVP,自帶範例

陳俊安 Chen Chun-An

Android & Web Developer passionate about UX & Startup. My Dream is to create user-friendly products that could satisfy people and fulfill their imagination about future. 喜歡思考人生的意義。夢想是做出能滿足人們對於未來想像的產品

comments powered by Disqus