AndroidPub
Published in

AndroidPub

Architecture patterns in Android — Android architecture design

Image Credit: https://unsplash.com/@chrisabney

Checkout my latest app: Tinido.

One of the big mistakes I have made is that I did not choose any proper architecture design from the starting point for many projects.

But why does your app need good architecture?

A simple answer is that everything should be organized in a proper way. So does your Android project.

If not, the following problems sound familiar to you:

  • All of your codes are not covered by Unit Tests.
  • It is difficult to debug a class because it contains a huge number of functions.
  • You are unable to keep track of the logic inside that huge class.
  • Another developer finds it so difficult to maintain and add new features to your work.

So, if you are going to build a high-quality app, you should care about architecture.

What does your app get from a proper architecture?

  • Simplicity: Separate and define a clear single role for each component in your app. A class is not going to be a multi-tasking component. You will find it easy to know what it does and what is inside it. It advocates the Keep It Stupid Simple (KISS).
  • Testability: Before we can apply Unit Tests, we have to write testable codes.
  • Low-cost maintenance: It is easy to add, and remove features. Especially, it helps us to keep track of important logic parts.

Several upcoming questions maybe appear in your head.

- So, what is the best architecture pattern for my Android apps?

- And how can I apply that pattern in the most effective way?

There is no single candidate that suits all of your Android projects because the design pattern is abstract and its implementation depends on specific requirements.

Fortunately, the more we understand about it, the more effectively and properly we apply them.

You can use different architectures across different apps. Even, in one complex project, each module has its own structure.

Another question?

So, if I have never used any architecture in my Android apps yet. So, what should I do?

Just pick up one of them. Read about it, try to apply it. After that, you will become familiar with it and have your own best practices.

Developers out there are talking about these following popular patterns:

  • MVC ( Model — View — Controller)
  • MVP ( Model — View — Presenter)
  • MVVM (Model — View — ViewModel)
  • Clean Architecture

These architecture patterns above made me really confused for the first time.

However, let’s go with one of them and see how it works, after that you can understand the remaining ones.

MVP in Android

MVP is strongly recommended because a lot of developers are using it now. Even, Google also provides its best practice example on Github.

The definition is not always an interesting part but it is vital. We should even check it out, again and again, to understand it deeply.

Image Credit: Tin Megali
  • View = a passive interface that displays data and routes user actions to the Presenter. In Android, it is represented by Activity, Fragment, or View.
  • Model = a layer that holds business logic controls how data is created, stored, and modified. In Android, it is a data access layer such as database API or remote server API.
  • Presenter = A middle man which retrieves data from Model and shows it in the View. It also processes user action forwarded to it by the View.

Important points of MVP are:

  • View can not access Model.
  • Presenter is tied to a single View. (One-to-One relationship)
  • View is completely dumb and passive (only retrieve user action and leave all other things for Presenter to handle).

A small example

This “hello-mvp” app has been defined by two following functions:

  • Save name: Get the first name and last name from EditText inputs
  • Show name: Load the saved name and show it on a TextView

A simple version of the Model look likes this.

The following contract keeps both functions of the View and the Presenter. It is a good guideline to keep track of their relationship.

You need a concrete Presenter class in implementation.

An implementation of the View is an Activity.(or a Fragment if you prefer that way).

An above example is just a simple approach of applying MVP to Android apps. Hope that you find your own best practices for your projects.

The full source code can be found on Github repository.

Finally, there are many resources about MVP in Android out there, but I highly recommend some of them which are listed below.

The more we learn about it, the more effective we implement it.

Besides, I also wrote other following articles related to MVP architecture topics. I hope that you find them useful.

Please get in touch with me via Github. Happy coding and have a good time!

--

--

--

The (retired) Pub(lication) for Android & Tech, focused on Development

Recommended from Medium

Laravel Routing — A step up

JetThoughts Is On GoodFirms Radar for Delivering Effortless Ruby on Rails Development Services to…

| Engineering News-Record

Laravel CRUD | Laravel 8 CRUD Application Tutorial for Beginners

Principles in Acceleration of Software Development

Valhalla Liquidity Launch Event

AZ-900 Exam Prep Notes: Part 1

CSS — The Styling Language.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Quang Nguyen

Quang Nguyen

Software engineer and designer, specializing in native mobile apps, currently building digital scent technology at aromajoin.com.

More from Medium

How to use TDD effectively to write android integration tests — Part 1 of 3

Automate your Android build code

Understanding Clean Architecture in Android Development

Integration of Huawei Ads Kit and Analytics Kit in Money Management Android app (Kotlin) — Part 2