📜 ⬆️ ⬇️

How to implement pure architecture on Android?

How to implement pure architecture on Android?


What do you find in this article?


In 2016, I started learning Java, and in early 2017, Android. From the very beginning, I already knew that there was a concept of application architecture, but I did not know how to apply it in my code. I found a lot of different guides, but this didn’t become clearer to me.


This article is the one that I would like to read at the beginning of my journey.


The importance of application architecture


Many companies conduct technical tests for candidates who are selected. Tests may differ, but there is something that unites them. All of them require an understanding and ability to apply a good software architecture.


A good software architecture makes it easy to understand, develop, maintain, and implement a system [The Pure Architecture Book, Chapter 15]

The purpose of this article is to teach someone who has never used the architecture for developing Android applications to do this. To do this, we consider a practical example of an application, during the creation of which we implement the least flexible solution and a more optimal solution using the architecture.


Example


Items in RecyclerView:


Items in RecyclerView


  1. We will receive data from the API and show the results to the user.
  2. The result will be a list of beers with a name, description, image, and alcohol content for each.
  3. Beer should be ordered by degree of strength.

To solve this problem:


  1. We need to get data from the API.
  2. Arrange items from lowest to highest degree of strength.
  3. If the alcohol content is less than 5%, a green circle will be drawn; if it is between 5% and 8%, the circle will be orange, and above 8% a red circle.
  4. Finally, we need to show a list of items.

What is the least flexible solution?


The least flexible solution is to create a single class that will fulfill the four previous points. This is the solution that any of us would make if we didn’t know what an application architecture is.


The result for the user will be acceptable: he will see an ordered list on the screen. But if we need to scale this system, we will understand that the structure is not easy to understand, further develop, maintain and implement.


How to understand the architecture of applications in Android?


I will give a very simple example. Imagine a car factory with five zones:


  1. The first zone creates the chassis.
  2. The second zone connects the mechanical parts.
  3. The third zone collects the electronic circuit.
  4. The fourth area is painting.
  5. And the last area adds aesthetic details.

This means that each zone has its own responsibility, and they work in a chain from the first zone to the fifth to achieve the result.


Such a system has a certain advantage. If the car gives an error after it is finished, then depending on its behavior, we will know which department should fix it, without disturbing others.


If we want to add more aesthetic details, we turn directly to the fifth section. And if we want to change the color, we turn to the fourth. And if we change the chassis, it will not change the way the paint area works. That is, we can point to modify our car, without disturbing the entire factory.


In addition, if over time we want to open a second factory to create a new car model, it will be easier to divide the work areas.


Application architecture in Android


We are going to ensure that there is no class that would do all the work alone: ​​request data from the API, sort it and display it. All of this will be distributed over several areas, called layers.


What are these layers?


Android architecture layers


For this example, we are going to create a pure architecture, which consists of three levels, which in turn will be divided into five:


  1. Presentation level
  2. The level of business logic.
  3. And the data level.

1. Level of performance


The presentation level is a user level, a graphical interface that captures user events and shows the results to it. It also checks that there is no formatting error in the user input, and the displayed data is displayed correctly.


In our example, these operations are divided between the user interface level and the ViewModel level:



Instead of using the ViewModel, we can use another layer that performs this function, it’s just important to understand the responsibilities of each layer.


In our example, the user interface layer displays the beer list, and the ViewModel layer reports the color you should use depending on the alcohol range.


2. The level of business logic


At this level are all the business requirements that the application must meet. For this, the necessary operations are performed here. In our example, this is the sorting of beers from the lowest to the highest fortress.


3. Level of data


This level contains data and a way to access it.


These operations are divided between the repository level and the data source level:



How do the layers interact?


Let's look at the theoretical and practical approaches of interaction.


In theory:


The interaction between the layers


Each layer should only communicate with its immediate neighbors. In this case, if we look at the software architecture diagram:



On practice:


Package structure in the IDE Android Studio with a clean architecture:


Package structure


We have a package structure in which classes are created, each of which has its own area of ​​responsibility.


Final Notes on Application Architecture


We saw that each level of software architecture has its own area of ​​responsibility and they are all interconnected.


It is important to emphasize that I have never mentioned the libraries or programming languages ​​used, since the architecture is oriented towards the correct structuring of the code so that it is scalable. Over time, libraries change, but the principles of architecture are preserved.


Further, it is recommended to read about dependency injection to avoid instantiating objects directly in architecture classes and, thus, be able to perform unit testing using Mockito and JUnit.


I am sharing a repository where you can see:



')

Source: https://habr.com/ru/post/459402/


All Articles