SlideShare une entreprise Scribd logo
1  sur  117
Télécharger pour lire hors ligne
@FMuntenescu
A Journey Through MV
Wonderland
@FMuntenescu
Robust, stable, testable,
modular, easy to extend
@FMuntenescu
MV ( C P VM )
@FMuntenescu
MV ( C P VM )
Model
@FMuntenescu
MV ( C P VM )
View
Model
@FMuntenescu
MV ( C P VM )
View
Model
@FMuntenescu
MV ( C P VM )
View
Model
@FMuntenescu
Model – View – Controller
@FMuntenescu
Controller
Model
View
1	…	1
@FMuntenescu
Controller
Model
View
User	input
@FMuntenescu
Update	Model
Controller
Model
View
User	input
@FMuntenescu
Update
Update	Model
Controller
Model
View
User	input
@FMuntenescu
Query	latest	data
Update
Update	Model
Controller
Model
View
User	input
@FMuntenescu
Model – View – Controller
in Android
@FMuntenescu
Activity
Controller
Model
View
1	…	1
@FMuntenescu
Controller
Model
View
1	…	1
@FMuntenescu
Controller
Model
View
User	input
@FMuntenescu
Inform	about	input
Controller
Model
View
User	input
@FMuntenescu
Update	Model
Inform	about	input
Controller
Model
View
User	input
@FMuntenescu
Update
Update	Model
Inform	about	input
Controller
Model
View
User	input
@FMuntenescu
Query	latest	data
Update
Update	Model
Inform	about	input
Controller
Model
View
User	input
@FMuntenescu
IView
Controller
Model
View
1	…	1
@FMuntenescu
Who handles UI logic?
@FMuntenescu
class User {
String firstName;
String lastName;
…
}
@FMuntenescu
class User {
String firstName;
String lastName;
…
}
displayName =
lastName + “, “ + firstName
@FMuntenescu
class User {
String firstName; Jane
String lastName; Doe
…
}
displayName = Doe, Jane
lastName + “, “ + firstName
@FMuntenescu
View?
User user = userModel.getUser();
nameTextView.setText(
user.getLastName()
+ ", "
+ user.getFirstName())
@FMuntenescu
Model?
String name = userModel.getDisplayName();
nameTextView.setText(name);
@FMuntenescu
The View knows too much!
@FMuntenescu
IView
Controller
Model
View
1	…	1
@FMuntenescu
IView
Controller
Model
View
1	…	1
@FMuntenescu
Controller ModelView
1	…	1
IView
@FMuntenescu
Presenter ModelView
1	…	1
IView
@FMuntenescu
Model – View – Presenter
@FMuntenescu
Presenter ModelView
1	…	1
IView IPresenter
@FMuntenescu
Contract
Presenter ModelView
1	…	1
IView IPresenter
@FMuntenescu
interface IView {
void setName(String name);
}
@FMuntenescu
IUserModel userModel;
IView view;
Presenter(IUserModel userModel,
IView view) {
this.userModel = userModel;
this.view = view;
}
@FMuntenescu
IUserModel userModel;
IView view;
Presenter(IUserModel userModel,
IView view) {
this.userModel = userModel;
this.view = view;
}
@FMuntenescu
interface IPresenter {
void onLoad();
}
@FMuntenescu
@Override
public void onLoad() {
User user = userModel.getUser();
String displayName =
user.getFirstName()
+ ", "
+ user.getLastName();
view.setName(displayName);
}
@FMuntenescu
@Override
public void onLoad() {
User user = userModel.getUser();
String displayName =
user.getFirstName()
+ ", "
+ user.getLastName();
view.setName(displayName);
}
@FMuntenescu
@Override
public void onLoad() {
User user = userModel.getUser();
String displayName =
user.getFirstName()
+ ", "
+ user.getLastName();
view.setName(displayName);
}
@FMuntenescu
@Mock
IUserModel userModel;
@Mock
IView view;
Presenter presenter;
@Before
public void setup() {
…
presenter =
new Presenter(userModel, view);
}
@FMuntenescu
@Mock
IUserModel userModel;
@Mock
IView view;
Presenter presenter;
@Before
public void setup() {
…
presenter =
new Presenter(userModel, view);
}
@FMuntenescu
@Test
public void displayName_setOnLoad() {
when(userModel.getUser())
.thenReturn(new User(“Jane”, “Doe”);
presenter.onLoad();
verify(view).setName(“Doe, Jane”);
}
@FMuntenescu
@Test
public void displayName_setOnLoad() {
when(userModel.getUser())
.thenReturn(new User(“Jane”, “Doe”);
presenter.onLoad();
verify(view).setName(“Doe, Jane”);
}
@FMuntenescu
@Test
public void displayName_setOnLoad() {
when(userModel.getUser())
.thenReturn(new User(“Jane”, “Doe”);
presenter.onLoad();
verify(view).setName(“Doe, Jane”);
}
@FMuntenescu
@Test
public void displayName_setOnLoad() {
when(userModel.getUser())
.thenReturn(new User(“Jane”, “Doe”);
presenter.onLoad();
verify(view).setName(“Doe, Jane”);
}
@FMuntenescu
Presenter ModelView
1	…	1
IView IPresenter
@FMuntenescu
IUserModel userModel;
IView view;
Presenter(IUserModel userModel, IView view) {
this.userModel = userModel;
this.view = view;
}
void onLoad() {
…
view.setName(displayName);
}
@FMuntenescu
IUserModel userModel;
IView view;
Presenter(IUserModel userModel, IView view) {
this.userModel = userModel;
this.view = view;
}
void onLoad() {
…
view.setName(displayName);
}
Observable<String> getName() {
…
}
@FMuntenescu
class View {
…
viewModel.getName()
.subscribe(name-> setName(name));
…
private void setName(String name){
nameTextView.setText(name);
}
@FMuntenescu
IUserModel userModel;
IView view;
Presenter(IUserModel userModel,
IView view) {
userModel = userModel;
view = view;
}
@FMuntenescu
interface IView {
void setName(String name);
}
@FMuntenescu
Presenter ModelView
1	…	*
IView IPresenter
@FMuntenescu
ViewModel ModelView
1	…	*
@FMuntenescu
Model – View – ViewModel
@FMuntenescu
ViewModel ModelView
1	…	*
@FMuntenescu
class User {
String firstName;
String lastName;
Date dateOfBirth;
}
@FMuntenescu
class User {
String firstName; Jane
String lastName; Doe
Date dateOfBirth; 04.04.1987
}
@FMuntenescu
class User {
String firstName; Jane
String lastName; Doe
Date dateOfBirth; 04.04.1987
}
Name: Doe, Jane
Age: 29
@FMuntenescu
class ViewModel {
Observable<String> getName() {
…
}
Observable<Integer> getAge() {
…
}
}
@FMuntenescu
class ViewModel {
Observable<DisplayableUser> getUser() {
…
}
}
@FMuntenescu
class DisplayableUser {
String mName;
int mAge;
}
class ViewModel {
Observable<DisplayableUser> getUser() {
…
}
}
@FMuntenescu
@Mock
IUserModel userModel;
ViewModel viewModel;
@Before
void setup() {
…
viewModel = new ViewModel(userModel);
}
@FMuntenescu
@Mock
IUserModel userModel;
ViewModel viewModel;
@Before
public void setup() {
…
viewModel = new ViewModel(userModel);
}
@FMuntenescu
@Test
public void getUser_emitsCorrectValue() {
when(userModel.getUser())
.thenReturn(new User(“Jane”,”Doe”,
new Date(4,4,1987));
viewModel.getUser()
.subscribe(testSubscriber);
testSubscriber.assertValue(expectedUser);
}
@FMuntenescu
@Test
public void getUser_emitsCorrectValue() {
when(userModel.getUser())
.thenReturn(new User(“Jane”,”Doe”,
new Date(4,4,1987));
viewModel.getUser()
.subscribe(testSubscriber);
testSubscriber.assertValue(expectedUser);
}
@FMuntenescu
@Test
public void getUser_emitsCorrectValue() {
when(userModel.getUser())
.thenReturn(new User(“Jane”,”Doe”,
new Date(4,4,1987));
viewModel.getUser()
.subscribe(testSubscriber);
testSubscriber.assertValue(expectedUser);
}
@FMuntenescu
@Test
public void getUser_emitsCorrectValue() {
when(userModel.getUser())
.thenReturn(new User(“Jane”,”Doe”,
new Date(4,4,1987));
viewModel.getUser()
.subscribe(testSubscriber);
testSubscriber.assertValue(expectedUser);
}
@FMuntenescu
Model – View – Presenter
&
Model – View – ViewModel
@FMuntenescu
class Presenter {
void onLoad() {
…
view.setName(displayName);
}
}
class ViewModel {
Observable<String> getName() {
…
}
}
@FMuntenescu
Who is the View?
@FMuntenescu
(	P	|	VM	)Activity
@FMuntenescu
(	P	|	VM	)
Fragment
Activity
@FMuntenescu
(	P	|	VM	)
Fragment
Activity
Custom	View
Custom	View
@FMuntenescu
class View implements IView {
@Inject
Presenter presenter;
…
presenter.onLoad(this);
@FMuntenescu
class View {
@Inject
ViewModel viewModel;
@FMuntenescu
Fragment
SomeView
AnotherView
@FMuntenescu
<RelativeLayout>
<my.custom.SomeView />
<my.custom.AnotherView />
</RelativeLayout>
Fragment
SomeView
AnotherView
@FMuntenescu
<RelativeLayout>
<my.custom.SomeView />
<my.custom.AnotherView />
</RelativeLayout>
Fragment
SomeView
AnotherView
NewView
@FMuntenescu
<RelativeLayout>
<my.custom.SomeView />
<my.custom.NewView />
<my.custom.AnotherView />
</RelativeLayout>
Fragment
AnotherView
NewView
@FMuntenescu
How to split Views?
@FMuntenescu
Complexity
Responsibility
Reusability
@FMuntenescu
What goes where?
V ? ( P VM )
@FMuntenescu
Does it contain UI logic?
View( P | VM )
Yes No
@FMuntenescu
(	P	| VM	) ModelView
@FMuntenescu
(	P	| VM	) ModelView
@FMuntenescu
How to handle
dependencies between
Views?
@FMuntenescu
Fragment
View	1
View	2
@FMuntenescu
Fragment
View	1
View	2
View	1
View	2
@FMuntenescu
JaneFragment
View	1
View	2
Doe
4.4.1987
CONTINUE
@FMuntenescu
Fragment
View	1
View	2
Model
@FMuntenescu
Fragment Presenter
View	1 Presenter
View	2 Presenter
@FMuntenescu
Fragment ViewModel
View	1 ViewModel
View	2 ViewModel
@FMuntenescu
How to handle Android
lifecycle?
@FMuntenescu
onResume /
onPause
@FMuntenescu
@Override
protected void onResume() {
…
presenter.onLoad();
}
@Override
protected void onPause() {
presenter.onStopLoad();
…
}
@FMuntenescu
@Override
protected void onResume() {
…
presenter.onLoad();
}
@Override
protected void onPause() {
presenter.onStopLoad();
…
}
@FMuntenescu
@Override
protected void onResume() {
…
viewModel.getDataStream()
.subscribe(…);
}
@Override
protected void onPause() {
// unsubscribe
…
}
@FMuntenescu
@Override
protected void onResume() {
…
viewModel.getDataStream()
.subscribe(…);
}
@Override
protected void onPause() {
// unsubscribe
…
}
@FMuntenescu
onAttachedToWindow /
onDetachedFromWindow
@FMuntenescu
@Override
void onAttachedToWindow() {
presenter.onLoad();
…
}
@Override
void onDetachedFromWindow() {
…
presenter.onStopLoad();
}
@FMuntenescu
@Override
void onAttachedToWindow() {
presenter.onLoad();
…
}
@Override
void onDetachedFromWindow() {
…
presenter.onStopLoad();
}
@FMuntenescu
@Override
void onAttachedToWindow() {
viewModel.getDataStream()
.subscribe(…);
…
}
@Override
void onDetachedFromWindow() {
…
// unsubscribe
}
@FMuntenescu
@Override
void onAttachedToWindow() {
viewModel.getDataStream()
.subscribe(…);
…
}
@Override
void onDetachedFromWindow() {
…
// unsubscribe
}
@FMuntenescu
onSaveInstanceState /
onRestoreInstanceState
@FMuntenescu
@Override
void onSaveInstanceState(Bundle state) {
state.putAll(
presenter/viewModel.getState());
}
@Override
void onRestoreInstanceState(Bundle state) {
presenter/viewModel
.restoreState(state);
}
@FMuntenescu
@Override
void onSaveInstanceState(Bundle state) {
state.putAll(
presenter/viewModel.getState());
}
@Override
void onRestoreInstanceState(Bundle state) {
presenter/viewModel
.restoreState(state);
}
@FMuntenescu
@Override
Parcelable onSaveInstanceState() {
// save state
}
@Override
void onRestoreInstanceState(
Parcelable state) {
// restore state
}
@FMuntenescu
MVC vs MVP vs MVVM
@FMuntenescu
Are your Android classes
logic free?
@FMuntenescu
Can you unit test
everything?
@FMuntenescu
Do your classes do one
thing and one thing only?
@FMuntenescu
Robust, stable, testable,
modular, easy to extend
@FMuntenescu
A Journey Through MV
Wonderland
jobs@upday.com
Google	Architecture	Blueprints	
https://github.com/googlesamples/android-architecture
MVP	vs	MVVM	example
https://github.com/florina-muntenescu/MVPvsMVVM
https://upday.github.io/

Contenu connexe

Plus de Florina Muntenescu

Plus de Florina Muntenescu (8)

MVM - It's all in the (Implementation) Details
MVM - It's all in the (Implementation) DetailsMVM - It's all in the (Implementation) Details
MVM - It's all in the (Implementation) Details
 
MVM - It's all in the (Implementation) Details
MVM - It's all in the (Implementation) DetailsMVM - It's all in the (Implementation) Details
MVM - It's all in the (Implementation) Details
 
Tech Talks: You Do Have Something To Say! Why and How To Start
Tech Talks: You Do Have Something To Say! Why and How To Start Tech Talks: You Do Have Something To Say! Why and How To Start
Tech Talks: You Do Have Something To Say! Why and How To Start
 
Code Learn Share
Code Learn ShareCode Learn Share
Code Learn Share
 
Optimising The Performance Of VectorDrawables
Optimising The Performance Of VectorDrawablesOptimising The Performance Of VectorDrawables
Optimising The Performance Of VectorDrawables
 
Building a reactive mindset
Building a reactive mindsetBuilding a reactive mindset
Building a reactive mindset
 
The ABCs of RxJava
The ABCs of RxJavaThe ABCs of RxJava
The ABCs of RxJava
 
Model-View-ViewModel and RxJava
Model-View-ViewModel and RxJavaModel-View-ViewModel and RxJava
Model-View-ViewModel and RxJava
 

Dernier

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Dernier (20)

How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

A Journey Through MV Wonderland