Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Architecting Android Apps

Abhay Sood
October 15, 2016

Architecting Android Apps

This is a presentation for the talk given at BrlDroid DevFest 2016.

This talk tries to demystify what architecture means and why we should care about it. It tries to provide context to developers about why it's such an important aspect of software development.

Finally it shows how a particular architecture like MVP can be implemented in Android keeping in mind the goals that we want to achieve.

Abhay Sood

October 15, 2016
Tweet

More Decks by Abhay Sood

Other Decks in Technology

Transcript

  1. • What is architecture? • Why is it so important

    that nearly every Android event has a talk dedicated to it? • What are the Goals we want to achieve? • How to implement MVP in Android. • Sample Code Agenda
  2. “In most successful software projects, the expert developers working on

    that project have a shared understanding of the system design. This shared understanding is called architecture.” - Ralph Johnson What is architecture? And what you want in a project is to have a very good shared understanding. Not doing this can lead to a lot of chaos. Source (http://martinfowler.com/ieeeSoftware/whoNeedsArchitect.pdf)
  3. Why care? - Users don’t care about your architecture as

    they don’t see it, it’s not a user facing thing. - Economic point of view: we need to move fast else a competing product would take advantage... This is generally what your PMs will say.
  4. • Testable code • Shared understanding • Easy to change

    • Should allow offline usage for the app • Should allow multiple developers to work independently, modular codebase Goals for following an Architecture in Android
  5. What is MVP View (Activity & Fragment) Presenter (Reacts to

    User Actions) Model (Database and Network interactions)
  6. - No logic in the view - It just delegates

    every action to the presenter. - Takes commands from the presenter to change the state of the UI. View
  7. View class SomeActivity implements MvpView { public void onCreate(Bundle savedInstanceState)

    { presenter.onViewCreated(); } @OnClick(R.id.btn_action) public void onActionClick() { presenter.onActionClick(); } @Override public void showText(String text) { textView.setText(text); } }
  8. Presenter - Co-ordinator between the View and Model. - Takes

    actions from the View. - Delegates request for data or business logic to the Model. - We should be able to unit test the Presenter on the JVM.
  9. Presenter class SomePresenter { MvpView view; public void onViewCreated() {

    view.showProgressBar(); model.loadData(); } public void onDataLoaded(Data data) { view.hideProgressBar(); view.showData(data); } }
  10. - It’s the business logic of your application. - Interacts

    with the data sources - database, cache or network to provide data. - As most of these operations are asynchronous, the model should not be affected by the activity lifecycle. Model
  11. Communication between layers View (Activity & Fragment) Presenter (Reacts to

    User Actions) Model (Database and Network interactions) 1 2
  12. (View -> Presenter) - View has a reference to the

    presenter which it uses for calling methods directly on the Presenter. (Presenter -> View) - The View implements an interface to create a strong boundary between the Presenter and itself. - The Presenter uses this interface for communication - It is required for unit testing the Presenter. Communication between View - Presenter
  13. Communication between Presenter - Model (Presenter -> Model) - Presenter

    has a reference to the Model or its interface and calls methods directly on them. (Model -> Presenter) - As most of this communication has to be asynchronous, it gets a bit tricky. - Callbacks - RxJava - EventBus
  14. Pros Cons Callbacks Easy Difficult to maintain Event Bus Easy,

    Event Driven Not much control RxJava Lots High learning curve Model -> Presenter communication
  15. Useful Links - Google Architecture Blueprints https://github.com/googlesamples/android-architecture - Android DevSummit

    - Architect for UX https://www.youtube.com/watch?v=BlkJzgjzL0c - A simple MVP application by Anup Cowkur https://github.com/anupcowkur/MVPSample