Layered Architecture — Android app architecture by example Part 3/5
Software architecture is the art of drawing lines that I call boundaries — Robert C. Martin
This is the third article of a five-article series on Android app architecture:
- What is Architecture?
- Programming principles
- Layered Architecture (you are here)
- Clean Architecture
- Sample app
Architecture is about putting the classes behind boundaries and how they communicate with each other through the boundaries.
I guess at some point most of us would have wondered looking at the project tree structure folders in Android Studio, how these folders/packages are named? Why someone is naming a package service and other is using background? Packages play an important role in the architecture as it helps us to organize related classes and present them as an interface.
Our journey to implement an architecture knowingly or unknowingly starts when we start putting activities/fragments in ui package. This is the beginning where we acknowledge the need of an architecture.
As I have mentioned before, separation of concerns is one of the guiding principles of programming. By putting the classes in a package we introduce them as a single component. We make these classes focus on a single concern. This grouping of classes can be called as a layer. We create layers so that we can organize classes which serve the same purpose.
Layered architecture pattern comes naturally to us. We start putting similar classes into packages which can be called a layer. There could be n number of layers but at a minimum we require 3 layers.
- UI layer: Here we can put activities, fragments, adapters and any other classes related to the user interface.
- Domain layer: Here we can put the business logic of our app. The code which is exclusive to our app.
- Data layer: The database related classes and any other system service.
The goal in a layered architecture is to create boundaries where one layer only interacts with adjacent layer and this happens in a single direction.
This means UI layer can’t directly interact with Data layer. This isolation helps us to minimize the impact of changes. Although there are cases where we can have an open layer which can be bypassed but more or less we follow interaction to the adjacent layer in a single direction.
This is a very simple architecture pattern which can be implemented with minimal effort. This is the reason Google guide to app architecture also recommends a similar app architecture. This will help the beginners to start with an architecture.
In the next part we will see a more evolved architecture pattern: Clean Architecture.
📝 Read this story later in Journal.
👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.