Share
Embed
Fighting against Android for the Right to Use Composition
Поговорим о том, что не так с MVP и его старшим братом MVC. И разберем как обойти ошибки проектирования Android и писать (хотя бы немного) повторно используемый объектно-ориентированный код.
GDG SPb
November 22, 2017
Transcript
102 slides
-
Who am I? • 2+ years Android code monkey •
1+ year JVM back-end dev javanese.online @Miha-x64 @miha_x64 @Harmonizr at @kotlin_mobile and @javanese_questions ❤️OOP, FP, composition -
Procedural, object-oriented, functional switch-case if-else val mode: Int if (mode
== MODE_X) performX(); else preformDefault(); object polymorphism val mode: Mode mode.perform() functional polymorphism val perform: () → Unit perform() -
The most of Android code is boilerplate • Create a
Fragment • Inflate a View • Show/Hide ProgressBar • Show data How many times have you done it? -
Monolithic Fragment • View — inflate layout, find views, set
adapters; • State — onSaveInstanceState, onViewStateRestored -
Monolithic Fragment • View — inflate layout, find views, set
adapters; • State — onSaveInstanceState, onViewStateRestored; • Context — Activity, Resources, getSystemService -
Monolithic Fragment • View — inflate layout, find views, set
adapters; • State — onSaveInstanceState, onViewStateRestored; • Context — Activity, Resources, getSystemService; • Lifecycle — attach/create/start/resume/pause/stop/destroy/detach -
Monolithic Fragment • View — inflate layout, find views, set
adapters; • State — onSaveInstanceState, onViewStateRestored; • Context — Activity, Resources, getSystemService; • Lifecycle — attach/create/start/resume/pause/stop/destroy/detach; • userVisibleHint — visibility in ViewPager -
Monolithic Fragment • View — inflate layout, find views, set
adapters; • State — onSaveInstanceState, onViewStateRestored; • Context — Activity, Resources, getSystemService; • Lifecycle — attach/create/start/resume/pause/stop/destroy/detach; • userVisibleHint — visibility in ViewPager; • targetFragment, onActivityResult, onRequestPermissionResult. -
Before talking about MVP... MVP is MVC for UI. It
inherits MVC’s problems. -
Controller vs. SOLID • Is not single-responsible; • No interfaces
— cannot be decorated or substituted -
Controller vs. SOLID • Is not single-responsible; • No interfaces
— cannot be decorated or substituted; • Not segregated -
Controller vs. SOLID • Is not single-responsible; • No interfaces
— cannot be decorated or substituted; • Not segregated; • Implementation dependency -
Controller vs. SOLID • Is not single-responsible; • No interfaces
— cannot be decorated or substituted; • Not segregated; • Implementation dependency; • Does not represent a solid object, its behaviour and state -
Controller vs. SOLID • Is not single-responsible; • No interfaces
— cannot be decorated or substituted; • Not segregated; • Implementation dependency; • Does not represent a solid object, its behaviour and state. Is just a bunch of procedures. -
MVP: to Decompose Undecomposable A Fragment delegates to Presenter the
following items: • View; • State; • Context; • Lifecycle; • userVisibleHint; • targetFragment, onActivityResult, onRequestPermissionResult. -
MVP • View is dumb and has got broken encapsulation;
• View-Presenter abstraction has leaked -
MVP • View is dumb and has got broken encapsulation;
• View-Presenter abstraction has leaked; • View and Presenter are separated by means of interfaces but without any code reuse and polymorphism -
MVP • View is dumb and has got broken encapsulation;
• View-Presenter abstraction has leaked; • View and Presenter are separated by means of interfaces but without any code reuse and polymorphism; • Presenter is a bunch of procedures, like a Controller -
MVP • View is dumb and has got broken encapsulation;
• View-Presenter abstraction has leaked; • View and Presenter are separated by means of interfaces but without any code reuse and polymorphism; • Presenter is a bunch of procedures, like a Controller; • View and Presenter hold links to each other which is very strange and discouraged practice -
MVP • View is dumb and has got broken encapsulation;
• View-Presenter abstraction has leaked; • View and Presenter are separated by means of interfaces but without any code reuse and polymorphism; • Presenter is a bunch of procedures, like a Controller; • View and Presenter hold links to each other which is very strange and discouraged practice; • Presenter is logically tied to Android platform. -
Presenter implementation inheritance What about extending existing Presenter for similar
use-case? Inheritance is a procedural technique for code reuse. Yegor Bugayenko Favor composition over inheritance. Joshua Bloch -
Interactor / Use Case • No concrete and focused purpose
— responsible for nothing and everything -
Interactor / Use Case • No concrete and focused purpose
— responsible for nothing and everything; • Not a solid object, similarly to Controller. -
CLEAN or VIPER Each feature is scattered through these layers.
• Many files with many LOC -
CLEAN or VIPER Each feature is scattered through these layers.
• Many files with many LOC; • Hard to modify -
CLEAN or VIPER Each feature is scattered through these layers.
• Many files with many LOC; • Hard to modify; • Hard to trace changes -
CLEAN or VIPER Each feature is scattered through these layers.
• Many files with many LOC; • Hard to modify; • Hard to trace changes; • Single layer itself is useless -
CLEAN or VIPER Each feature is scattered through these layers.
• Many files with many LOC; • Hard to modify; • Hard to trace changes; • Single layer itself is useless. Typical sample of Baklava-code. -
Android The things are even worse because of Android rules:
• Fragments forbid composition -
Android The things are even worse because of Android rules:
• Fragments forbid composition; • One Fragment + Presenter per use case — no code reuse! -
Android The things are even worse because of Android rules:
• Fragments forbid composition; • One Fragment + Presenter per use case — no code reuse! • Fragment resolves its own dependencies, like singleton -
Android The things are even worse because of Android rules:
• Fragments forbid composition; • One Fragment + Presenter per use case — no code reuse! • Fragment resolves its own dependencies, like singleton; • Passing an argument deeply through flow based on Fragment stack is painful and unmaintainable. -
DI containers • Just adding noise and indirection bind(class).toInstance(instance) @Inject
@Autowired @Scope @Qualifier @ComponentScan @Module -
DI containers • Just adding noise and indirection; • Inject
different implementations of the same interfaces? -
DI containers • Just adding noise and indirection; • Inject
different implementations of the same interfaces? • This is a dead end: we’ll get scopes and qualifiers, configs which can break something somewhere, dependencies on implementations instead of abstractions -
DI containers • Just adding noise and indirection; • Inject
different implementations of the same interfaces? • This is a dead end: we’ll get scopes and qualifiers, configs which can break something somewhere, dependencies on implementations instead of abstractions; • This introduces semantics of dynamic weakly-typed languages. -
Android acts like a bad DI container Application Activity Activity
Service Fragment Fragment Child Fragment Fragment Fragment Fragment Fragment Child Fragment He-he -
What we need • Reuse Fragments or Presenters for similar
screens by means of object or functional composition Search books Effective Java, 3rd Edition by Joshua Bloch Java Generics FAQ by Angelika Langer Java Performance: The... by Scott Oaks Thinking in Java, 4th edit... by Bruce Eckel Kotlin in Action by D. Jemerov and S. Isakova Search books Effective Java, 3rd Edition by Joshua Bloch Java Generics FAQ by Angelika Langer Java Performance: The... by Scott Oaks Thinking in Java, 4th edit... by Bruce Eckel Kotlin in Action by D. Jemerov and S. Isakova Paradigms Books Websites Procedural Simple and Annoying Functional Fascinating and Crazy Train Procedural Old and digesting Object-Oriented Interfaces and Composition Procedural You can write it even in Java Amazon.com Expensive books GitHub.com My favourite social net Twitter.com Read programmers’ jokes StackOverflow.com Downvote stupid questions Javanese.online WTF is this? Search paradigms Search websites -
What we need • Reuse Fragments or Presenters for similar
screens by means of object or functional composition; • Escape onActivityResult and onRequestPermissionResult hell -
What we need • Reuse Fragments or Presenters for similar
screens by means of object or functional composition; • Escape onActivityResult and onRequestPermissionResult hell; • Allow hierarchical communication (parent-child) -
What we need • Reuse Fragments or Presenters for similar
screens by means of object or functional composition; • Escape onActivityResult and onRequestPermissionResult hell; • Allow hierarchical communication (parent-child); • Transparent work with userVisibleHint -
What we need • Reuse Fragments or Presenters for similar
screens by means of object or functional composition; • Escape onActivityResult and onRequestPermissionResult hell; • Allow hierarchical communication (parent-child); • Transparent work with userVisibleHint; • SOLID, reusable, cross-platform Presenters. -
route is an object endpoint is an object I am
an object we are objects https://github.com/JavaneseOnline/KRUD/blob/master/krud-core/src/main/kotlin/online/javanese/krud/crud/Crud.kt Refactored Controller -
Previous solution: «Safe Fragments» • All arguments required to be
Parcelable. OK for data and functional objects, not for Observables, DB or net connections -
Previous solution: «Safe Fragments» • All arguments required to be
Parcelable. OK for data and functional objects, not for Observables, DB or net connections; • Ugly constructors -
Previous solution: «Safe Fragments» • All arguments required to be
Parcelable. OK for data and functional objects, not for Observables, DB or net connections; • Ugly constructors; • Using Bundles -
Previous solution: «Safe Fragments» • All arguments required to be
Parcelable. OK for data and functional objects, not for Observables, DB or net connections; • Ugly constructors; • Using Bundles • possible typos in keys -
Previous solution: «Safe Fragments» • All arguments required to be
Parcelable. OK for data and functional objects, not for Observables, DB or net connections; • Ugly constructors; • Using Bundles • possible typos in keys; • possible heap pollution in getParcelable<T> -
Previous solution: «Safe Fragments» • All arguments required to be
Parcelable. OK for data and functional objects, not for Observables, DB or net connections; • Ugly constructors; • Using Bundles • possible typos in keys; • possible heap pollution in getParcelable<T>; • custom Map and List storage surprises. -
Object graph: good architecture Application Activity Activity Service Fragment Fragment
Child Fragment Fragment Fragment Fragment Fragment Child Fragment -
PresenterTag is a token for obtaining Presenter. Parcelable, can be
passed anywhere. Similar to URL-based navigation, but type-safe. -
Passing data back and forth <- Argument <- result handler
optional cancellation handler ^ -
Passing data back and forth <- Argument <- result handler
Handler functions survive config changes. Must be pure! Neither partial application nor capturing is allowed. optional cancellation handler ^ -
Requesting Permissions =- requestCode ^ permission result handler ^ permission
list =- show rationale -
What’s next? • Reuse Presenters; • Delegate tasks to small
objects; • Write cross-platform delegates when you need it -
What’s next? • Reuse Presenters; • Delegate tasks to small
objects; • Write cross-platform delegates when you need it; • Use ViewModels or whatever you want. -
Try, contribute, ask, complain https://github.com/Miha-x64/Flawless javanese.online @Miha-x64 @miha_x64 @Harmonizr at
@kotlin_mobile and @javanese_questions