Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Clean architecture on android

2,005 views

Published on

Clean architecture on android

Published in: Software
  • Be the first to comment

Clean architecture on android

  1. 1. Clean Architecture on Android Benjamin / 班傑明
  2. 2. A real-world app Fragment Adapter AsyncTask Network API client AsyncTask View View Data model
  3. 3. Challenge ● Hard to understand/maintain ○ The business logic is mixed in Activity/Fragment/Adapter ○ Loose cohesive, high coupled (低內聚,高耦合) ● Hard to test ○ Cannot fully test the logic in Activity/Fragment/Adapter ○ The UI testing is fragile. (Will build fail if there are content or server-side issues) ○ It costs around 30 minutes to finish our UI testing ○ It’s really frustrating and lose productivity when the tests keep failing (lots of 30+ minutes)
  4. 4. MVP From: http://www.captechconsulting.com/blogs/a-mvp-approach-to-lifecycle-safe-requests-with-retrofit-20-and-rxjava
  5. 5. MVP - Interfaces Program to an interface, not an implementation
  6. 6. MVP - View
  7. 7. MVP - View
  8. 8. MVP - Presenter
  9. 9. MVP - Model ● Model? NetworkAPIClient? ● Is MVP an Architecture pattern?
  10. 10. MVC/MVP/MVVM is NOT an Architecture ● MVP is not an architectural pattern, it’s only responsible for the presentation layer (delivery mechanism)
  11. 11. The Clean Architecture From: http://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html
  12. 12. The Clean Architecture (conti.) From Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg
  13. 13. Interactor - Application specific business rules ● Application specific business rules ● Use cases ● ex: Content aggregation logic for your app’s landing page, add product to cart, ... ● Regular Java object. No android framework dependency
  14. 14. Repository - Data access ● Application independent business rules ● Business object / Domain model ● Access data by network, memory cache and disk cache, has Android framework dependency ● ex: RestAPI, Preference, disk cache
  15. 15. Boundary ● No direct dependency between framework and Interactor ● Interactor can be well tested without framework
  16. 16. MVP - Presentation Layer ● Presenter: Define interfaces for all the UI interactions [Regular Java object, no android framework dependency]
  17. 17. The Clean Architecture on Android Fragment Adapter Network API client View View Presenter Interactor RepositoryViewModel DataCache Response Model
  18. 18. The Clean Architecture ● Independent of Frameworks ● Testable ● Allow major decisions(UI, framework, databases) to be deferred
  19. 19. Android application architecture: Get ready for the next billion! - Google I/O 2016 From https://www.youtube.com/watch?v=70WqJxymPr8
  20. 20. Testing ● Presentation layer ○ Expresso ● Domain layer (Use case/Interactor) ○ JUnit + mockito ● Data layer ○ Roboletric + JUnit + mockito
  21. 21. Prod build flavor Expresso + Mockito Fragment AdapterView View Presenter InjectionInteractor Repository
  22. 22. Expresso + Mockito (conti.) Mock build flavor Fragment AdapterView View Presenter InjectionMockInteractor Repository
  23. 23. Expresso + Mockito (conti.)
  24. 24. Reference ● Robert C Martin - Clean Architecture and Design https://www.youtube.com/watch?v=Nsjsiz2A9mg ● Architecting Android…The clean way? http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/ ● Android Application Architecture. Our journey from standard Activities and AsyncTasks to a modern MVP-based architecture powered by RxJava. https://labs.ribot.co.uk/android-application-architecture-8b6e34acda65#.zexgfy5tt ● Android Testing (Android Dev Summit 2015) https://www.youtube.com/watch?v=vdasFFfXKOY ● Android Testing Codelab https://codelabs.developers.google.com/codelabs/android-testing/index.html#0 ● Advanced Android Espresso http://chiuki.github.io/advanced-android-espresso/#/

×