Share
Embed
MV–WTF?
A survey of presentation architectures, focused on options for Android.
Kevin Carpenter
April 30, 2016
Transcript
95 slides
-
Entities Use Cases Controllers Gateways Presenters Devices Web DB UI
External Interfaces Enterprise Business Rules Interface Adapters Frameworks & Drivers Application Business Rules -
M is for Model Entities Use Cases Controllers Gateways Presenters
Devices Web DB UI External Interfaces GET https://hacker-news.com/item/11595667.json { "by": "jamesblonde", "descendants": 0, "id": 11595667, "score": 2, "time": 1461937655, "title": “Make email great again”, "type": "story", "url": “https:\/\/email.net/great” } -
M is for Model Entities Use Cases Controllers Gateways Presenters
Devices Web DB UI External Interfaces { "id": 11595667, "by": "jamesblonde", "descendants": 0, "score": 2, "time": 1461937655, "title": "Successful way to stop spam email from ResearchGate", "type": "story", "url": "https:\/\/www.researchgate.net\/post" } CREATE TABLE IF NOT EXISTS ItemTable( id TEXT PRIMARY KEY NOT NULL, by TEXT NOT NULL, descendants INTEGER NOT NULL, score INTEGER NOT NULL, time INTEGER NOT NULL, title TEXT NOT NULL, type TEXT NOT NULL, url TEXT NOT NULL ) -
M is for Model Entities Use Cases Controllers Gateways Presenters
Devices Web DB UI External Interfaces interface Model { fun getItems(): Observable<List<Item>> } -
WTF is for… Controllers Presenters Devices Web DB UI External
Interfaces Presentation Models model.getItems() -
–“Uncle” Bob Martin “Gather together the things that change for
the same reasons. Separate those things that change for different reasons.” -
–Martin Fowler “Ensure that any code that manipulates presentation only
manipulates presentation, pushing all domain and data source logic into clearly separated areas of the program.” -
class ItemListViewImpl : RecyclerView, ItemListView, OnRefreshListener { // lifecycle, constructor,
etc… // OnRefreshListener implementation override fun onRefresh() { setLoadingAnimation(true) controller.refreshItems() } } User Controller View Model -
User Controller View Model class ItemListViewImpl : RecyclerView, ItemListView, OnRefreshListener
{ // lifecycle, constructor, etc… // OnRefreshListener implementation override fun onRefresh() { setLoadingAnimation(true) controller.refreshItems() } } -
class Controller() { @Inject private lateinit var model: Model
private var view: IItemListView? = null fun refreshItems() { model.updateItems() .subscribe( { view?.notifyUpdated() }, { view?.notifyError() }) } } User Controller View Model -
User Controller View Model class Controller() { @Inject private
lateinit var model: Model private var view: IItemListView? = null fun refreshItems() { model.updateItems() .subscribe { view?.notifyUpdated() } } } -
class Controller() { @Inject private lateinit var model: Model
private var view: IItemListView? = null fun refreshItems() { model.updateItems() .subscribe { view?.notifyUpdated() } } } User Controller View Model -
class ItemListViewImpl : RecyclerView, ItemListView, OnRefreshListener { // lifecycle, constructor,
etc… // OnRefreshListener implementation override fun onRefresh() {/*...*/} fun notifyUpdated() { val items = model.getItems() adapter.items = items setLoadingAnimation(false) } } User Controller View Model -
class ItemListViewImpl : RecyclerView, ItemListView, OnRefreshListener { // lifecycle, constructor,
etc… // OnRefreshListener implementation override fun onRefresh() {/*...*/} fun notifyUpdated() { val items = model.getItems() adapter.items = items setLoadingAnimation(false) } } User Controller View Model -
class ItemListViewImpl : RecyclerView, ItemListView, OnRefreshListener { // lifecycle, constructor,
etc… // OnRefreshListener implementation override fun onRefresh() {/*...*/} fun notifyUpdated() { val items = model.getItems() adapter.items = items setLoadingAnimation(false) } } User Controller View Model -
Testing MVC class TestViewImpl(private val model: Model) : IItemListView {
var items = emptyList<Item>() var loading = true override fun notifyUpdated() { this.items = model.getItems() this.loading = false } } class ModelImpl(private val items: List<Item>): Model { override fun fetchItems(): ListItems { return Observable.just(items) } override fun getItems(): List<Items> { return items } } -
Testing MVC class TestViewImpl(private val model: Model) : IItemListView {
var items = emptyList<Item>() var loading = true override fun notifyUpdated() { this.items = model.getItems() this.loading = false } } class ModelImpl(private val items: List<Item>): Model { override fun fetchItems(): ListItems { return Observable.just(items) } override fun getItems(): List<Items> { return items } } -
Testing MVC @Test fun test_getItemsError_notifiesError() { val items = ItemListFactory.makeList()
val model = ModelImpl(items) val view = TestViewImpl() val controller = ListController(model, view) controller.refreshItems() // assume blocking assertTrue(view.items = items) assertFalse(view.loading) } -
MVC Takeaways User Controller View Model • Controller has too
many responsibilities. -
MVC Takeaways User Controller View Model • Controller has too
many responsibilities. • The View knows about the Model and the Controller. -
MVC Takeaways User Controller View Model • Controller has too
many responsibilities. • The View knows about the Model and the Controller. • Testing could be easier. -
User Presenter View class ItemListView : RecyclerView, IItemListView, OnRefreshListener {
override fun onRefresh() { presenter.onRefresh() } } Model -
Model User Presenter View class ItemListView : RecyclerView, IItemListView, OnRefreshListener
{ override fun onAttachedToWindow() { super.onAttachedToWindow() presenter.attach(this) } override fun onDetachedFromWindow() { presenter.detach() super.onDetachedFromWindow() } override fun onRefresh() { presenter.onRefresh() } } -
Model User Presenter View class ItemListView : RecyclerView, IItemListView, OnRefreshListener
{ override fun onAttachedToWindow() { super.onAttachedToWindow() presenter.attach(this) } override fun onDetachedFromWindow() { presenter.detach() super.onDetachedFromWindow() } override fun onRefresh() { presenter.onRefresh() } } -
User Presenter View Model interface IItemListView { fun setLoading(loading: Boolean)
fun setItems(items: List<Item>) } class ItemListViewPresenter { fun attach(view: IItemListView) { this.view = view } fun detach() { this.view = null } fun onRefresh() { view?.setLoading(true) model.getItems() .subscribe( { items -> view?.setItems(items) },//onNext { view?.setLoading(false) }, //onError { view?.setLoading(false) })//onComplete } } -
User Presenter View Model interface IItemListView { fun setLoading(loading: Boolean)
fun setItems(items: List<Item>) } class ItemListViewPresenter { fun attach(view: IItemListView) { this.view = view } fun detach() { this.view = null } fun onRefresh() { view?.setLoading(true) model.getItems() .subscribe( { items -> view?.setItems(items) },//onNext { view?.setLoading(false) }, //onError { view?.setLoading(false) })//onComplete } } -
Testing MVP @Test fun test_refreshItems_setsViewItems() { val items = ItemListFactory.makeList()
val model = mock(Model::class.java) mockWhen(model.getItems()).thenReturn(Observable.just(items)) val view = mock(IItemListView::class.java) val presenter = ItemListViewPresenter(model, view) presenter.refreshItems() // assume blocking verify(view).setItems(items) verify(view).setLoading(false) } -
MVP Takeaways • The Presenter has the same responsibilities as
before. User Presenter View Model -
MVP Takeaways User Presenter View Model • The Presenter has
the same responsibilities as before. • The View no longer knows about the Model. -
MVP Takeaways User Presenter View Model • Presenter has the
same responsibilities as before. • View no longer knows about the Model. • Presenter is in full control of what is rendered in View. -
MVP Takeaways User Presenter View Model • Presenter has the
same responsibilities as before. • View no longer knows about the Model. • Presenter is in full control of what is rendered in View. • Testing is still tough. We need to thoroughly mock IItemListView to unit test. -
MVP View stores its own state User Presenter View Model
class ItemListView : RecyclerView, IItemListView, OnRefreshListener { private var items: List<Items> = emptyList() private var loading: Boolean = false override fun setItems(newItems: List<Items>) { this.items = newItems // update items } override fun setLoading(loading: Boolean) { this.loading = loading // show or hide progress bar } } -
MVP View stores its own state User Presenter View Model
class ItemListView : RecyclerView, IItemListView, OnRefreshListener { private var items: List<Items> = emptyList() private var loading: Boolean = false override fun onSaveInstanceState(): Parcelable { // ☠☠☠☠☠☠☠☠ } override fun setItems(newItems: List<Items>) { this.items = newItems } override fun setLoading(loading: Boolean) { this.loading = loading } } -
Presentation Model stores state User PM View Model class ItemListViewModel
{ private var model: Model private var loading: Boolean private var items: List<Item> fun onRefresh() { loading = true items = model.getItems() // communicate to View } } -
User ViewModel View Model class ItemListView: RecyclerView, OnRefreshListener { override
fun onRefresh() { viewModel.onRefresh() } } -
Model User ViewModel View class ItemListView: RecyclerView, OnRefreshListener { override
fun onRefresh() { viewModel.onRefresh() } } -
User ViewModel View class ItemListViewModel { private var model: Model
private var loadingSubject = PublishSubject.create<Boolean private var itemsSubject = PublishSubject.create<List<Item fun onRefresh() { loadingSubject.onNext(true) model.getItems() .subscribe( { items -> itemsSubject.onNext(items) }, // onNext { loadingSubject.onNext(false) }, // onError { loadingSubject.onNext(false) }) // onComplete } } Model -
User ViewModel View Model class ItemListViewModel { private var model:
Model private var loadingSubject = PublishSubject.create<Boolean private var itemsSubject = PublishSubject.create<List<Item fun onRefresh() { loadingSubject.onNext(true) model.getItems() .subscribe( { items -> itemsSubject.onNext(items) }, // onNext { loadingSubject.onNext(false) }, // onError { loadingSubject.onNext(false) }) // onComplete } } -
User ViewModel View class ItemListView { override fun onAttachedToWindow()
{ super.onAttachedToWindow() viewModel.loadingSubject .subscribe(loadingObserver) viewModel.itemsSubject .subscribe(itemsObserver) } } Model -
User ViewModel View class ItemListView { private val loadingObserver
= { loading: Boolean -> progressBar.visibility = if (loading) { View.VISIBLE } else { View.GONE } } private val itemsObserver = { items: List<Item> -> adapter.items = items } } Model -
Testing MVVM @Test fun test_getItems_updatesItems() { val items = ItemListFactory.makeList()
val model = mock(Model::class.java) mockWhen(model.getItems()).thenReturn(Observable.just(items)) val viewModel = ItemListViewModel(model) var updatedItems = emptyList<Item>() val itemsSubscriber = { newItems -> items = newItems } var loading = true val loadingSubscriber = { loadingUpdate -> loading = loadingUpdate} viewModel.itemsSubject.subscribe(itemsSubscriber) viewModel.loadingSubject.subscribe(loadingSubscriber) viewModel.refreshItems() // assume blocking assertEquals(items, updatedItems) assertFalse(loading) } -
MVVM Takeaways • The ViewModel is still the “middle man”
User ViewModel View Model -
MVVM Takeaways • The ViewModel is still the “middle man”
• Clearly defined code paths User ViewModel View Model -
MVVM Takeaways • The ViewModel is still the “middle man”
• Clearly defined code paths • The View observes changes in the ViewModel User ViewModel View Model -
MVVM Takeaways • The ViewModel is still the “middle man”
• Clearly defined code paths • The View observes changes in the ViewModel • Unit testing is improved! User ViewModel View Model -
Data Binding class User( var firstName: String, var lastName: String)
class UserView { private val userBinding constructor(/*...*/) { val inflater = LayoutInflater.from(context) userBinding = UserViewBinding.inflate(inflater) } fun updateUser(newUser: User) { userBinding.update(newUser) } } -
Data Binding <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="user" type=“devfest.example.User”/> </data> <LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=“@{user.firstName}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=“@{user.lastName}" /> </LinearLayout> </layout> -
Sources • External read effects • User input • E.g.
Pull to Refresh Refresh Events time -
Sinks • Write effects to external world • Data model
output • E.g. formatted data response, like our list of Items Item List Update Events time -
Cycle • UIs are cycles Source: What if the user
was a function? by Andre Staltz at JSConf Budapest 2015 ☝ -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions • UIs are asynchronous Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions • UIs are asynchronous Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions • UIs are asynchronous Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions • UIs are asynchronous Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions • UIs are asynchronous Pull to Refresh List Updates ViewModel: f(refresh) = list updates -
Source: What if the user was a function? by Andre
Staltz at JSConf Budapest 2015 Pull to Refresh List Updates ViewModel: f(refresh) = list updates • UIs are cycles • UIs are functions • UIs are asynchronous Cycle -
Cycle Source: What if the user was a function? by
Andre Staltz at JSConf Budapest 2015 • UIs are cycles • UIs are functions • UIs are asynchronous • UIs are symmetric ViewModel: f(refresh) = list updates -
Cycle • UIs are cycles • UIs are functions •
UIs are asynchronous • UIs are symmetric • The user is a function Source: What if the user was a function? by Andre Staltz at JSConf Budapest 2015 ViewModel: f(refresh) = list updates -
interface Model<I : Sources, O : Sinks> { fun setUp(sources:
I): O fun tearDown() interface Sources interface Sinks } User Reactive Presentation Model Model View -
class Sinks( val items: Observable<List<Item>>, val loading: Observable<Boolean>) : Sinks
User Reactive Presentation Model Model View -
class ItemListViewModel : Model<Sources, Sinks> { class Sources( val refreshEvents:
Observable<Unit>) : Sources class Sinks( val items: Observable<List<Item>>, val loading: Observable<Boolean>) : Sinks } User Reactive Presentation Model Model View -
class ItemListView : RecyclerView, OnRefreshListener { @Inject private lateinit
var model: ItemListViewModel private val progressBar //... private val adapter = ItemListViewAdapter() private val subscriptions = CompositeSubscription() private val refreshSubject = PublishSubject.create<Unit>() override fun onAttachedToWindow() { super.onAttachedToWindow() val sinks = model.setUp(Sources(refreshSubject.asObservable())) sinks.loading.subscribe(loadingObserver).addToSubscriptions() sinks.items.subscribe(itemsObserver).addTo(subscriptions) } } User Reactive Presentation Model Model View -
class ItemListViewModel : Model<Sources, Sinks> { class Sources( val
itemUrlClicks: Observable<Item>, val itemDetailClicks: Observable<Item>) : Model.Sources class Sinks(val items: Observable<List<Item>>) : Model.Sinks } -
class ItemDetailViewModel : Model<Sources, Sinks> { class Sources( val itemUrlClicks:
Observable<Item>, val itemDetailClicks: Observable<Item>) : Model.Sources class Sinks(val items: Observable<List<Item>>) : Model.Sinks } -
class ItemDetailViewModel : Model<Sources, Sinks> { class Sources( val
backClicks: Observable<Unit>, val shareClicks: Observable<Unit>) : Model.Sources class Sinks( val item: Observable<Item>, val children: Observable<List<Item>>) : Model.Sinks }