Develop an Android project architectural design pattern.

The reusability of code with a project architectural design pattern for Android Developers.

Narendra Harny
Make Android
7 min readAug 4, 2020

--

Photo by Mohamed Nohassi on Unsplash

I have witnessed the ability of a project architectural design pattern when I have implemented one and, I got momentum in my development. It just happens because of a good design pattern I began reusing a lot of code.
I used MVP design and I did not limit myself to implementing my approach inside it.

Before starting any project we should think about maintaining the project’s architectural design pattern.

Many improvements for development are associated with a good project architectural design pattern.

  1. Good maintenance can be done.
  2. Reusability of modules.
  3. Fast CR Implementations.
  4. Adding a new module is easy.
  5. Removes redundant code as much as possible.
  6. The main advantage of the architectural design pattern is the source code will be easy to understand.
  7. It speeds up and makes development easy for developers.
  8. Clean source code.

I am going to accord perspectives about a customized architectural design pattern on top of MVP.
It is our view on how we should manage our project design, We don’t have to obey any design practices strictly because if we don’t keep ourselves restricted we can make a unique or better design for our projects.
The Android application developer needs to design many screen layouts. They have to design an Activity or Fragments for around all the screens and from all the screens there will be a network call to the server, This is very common in all Android applications.
I have used the MVP architecture and designed many applications and found good outcomes with that like all the above-listed advantages.
I will explain how it was implemented, It is anyhow like MVP and This design will be very useful for a common network call code for all the screens We are not going to write the network call code for, a second time in the application source code.

If anyone does not know about MVP then please read about MVP Design pattern.

In a usual way, an Android developer who develops an application without any architectural design pattern creates Activity or Fragments and writes all the code in the same activity. There is no rule for writing the number of lines in one file but it is good that it should not exceed 1000 lines.

So let’s design the project and see the difference when we have a good design pattern.

Prerequisite: Android Studio

Step1:

  • Create a new Android project with an empty Activity.
Initially project was without any design pattern

Here I have just created an Android project, where one activity is created, without any design pattern we have the freedom to start writing all the code into the same activity now even if it is a network call-related code or view access.

“But we won’t do that now, we are going to create the project architectural design pattern and will reuse the network calling module everywhere in the project”

Step2:

  • The current package for me is “com.example.project-design pattern” It’s better to maintain the same for the exercise.
  • Suppose we have the Registration module/screen called registration.
  • Create the following files into the project and change them as shown in the image below.
  1. Add a package/folder called “registration”
  2. RegistrationActivity.java
  3. IRegisrationPresenter.java
  4. IRegistrationView.Java
  5. RegistrationPresenter.java
  6. XML for registration activity.

Now we have created the structure for one screen/activity, and we can follow it for other activities also, It is just to add a package/folder with the name of the respective module name, and always two interfaces and one presenter java class will be added for each screen.

Setp3:

  • Now write the following code as mentioned below in all the files one by one.

RegistrationActivity.java

IRegisrationPresenter.java

IRegistrationView.Java

RegistrationPresenter.java

activity_registaration.xml

The architectural view of the implementation so far.

Design so far…

As per the architecture so far:

  • Activity >> calls Presenter interface and send view data>> Interface will be implemented by the Presenter class.
  • The presenter is a java class that will execute all the business logic and network call-related code.
  • Activities contain the code of accessing UI elements and passing data to the presented via the presenter interface.
  • Methods of the Presenter will be accessed by Activity and Activity data will be passed to the presenter class for processing network calling.

We have implemented half part of the design pattern so far so let’s go further.

Step4:

Add the following structure into the code and the project structure will be like this:

final project structure

Newly added files into a design pattern.

  1. package “network call
  2. IrequestNetworkCalle.java interface
  3. IResponseNetworkCaller.java
  4. NetworkCaller.java
  5. added some Gradle dependencies for retrofit support.

Note: Gradle dependency is to understand the implementation batter throughout the project and how we can use a common network calling code.

Let’s edit the code one more time.

RegistrationActivity.java

IRegisrationPresenter.java

IRegistrationView.Java

RegistrationPresenter.java

IrequestNetworkCaller.java interface

IResponseNetworkCaller

NetworkCaller.java

build.gradle

Above Code is the final project architecture and this can be used.

So now I can explain the full architecture with network calling and how it will be reused always.

Project architecture:

The complete project architecture

The above pattern is ready for one screen and one can keep on adding screens to it, I am explaining the sequence diagram of all the components so that the responsibility of all the components will be clear and one can map the sequence diagram with the code so it will help to understand it easily.

Thanks for Reading! If this article was helpful. Please hit the clap!

Follow on medium https://medium.com/@narendra147h

Connect on Linked In: linkedin.com/in/narendraharny

Please Follow & subscribe to Make Android Publication by Narendra K H for prompt updates on Android platform-related blogs.

Thank you!

--

--

Narendra Harny
Make Android

Connect on https://medium.com/make-android | Write On, Android AOSP & Applications | Python | DevOps | Java | C++ | Kotlin | Shell | Linux | Android Auto | IVI