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.
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.
Items in RecyclerView:
To solve this problem:
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.
I will give a very simple example. Imagine a car factory with five zones:
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.
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.
For this example, we are going to create a pure architecture, which consists of three levels, which in turn will be divided into five:
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.
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.
This level contains data and a way to access it.
These operations are divided between the repository level and the data source level:
Let's look at the theoretical and practical approaches of interaction.
Each layer should only communicate with its immediate neighbors. In this case, if we look at the software architecture diagram:
Package structure in the IDE Android Studio with a clean architecture:
We have a package structure in which classes are created, each of which has its own area of ​​responsibility.
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