Android Clean Architecture Flashcards

1
Q

Why Clean Architecture Is Important ?

A

https://proandroiddev.com/kotlin-clean-architecture-1ad42fcd97fa

All architectures have one common goal — to manage the complexity of your application. You may not need to worry about it on a smaller project, but it becomes a lifesaver on larger ones.

How does Clean Architecture approach the problem?

The circles represent different levels of software in your app. There are two key things to note:

  • The center circle is the most abstract, and the outer circle is the most concrete. This is called the Abstraction Principle. The Abstraction Principle specifies that inner circles should contain business logic, and outer circles should contain implementation details.
  • Another principle of Clean Architecture is the Dependency Rule. This rule specifies that each circle can depend only on the nearest inward circle — this is what makes the architecture work.

The outer circle represents the concrete mechanisms that are specific to the platform such as networking and database access. Moving inward, each circle is more abstract and higher-level. The center circle is the most abstract and contains business logic, which doesn’t rely on the platform or the framework you’re using.

Additional benefits of using an architecture when structuring app code include:

  • Parts of the code get decoupled, and easier to reuse and test.
  • There’s a method to the madness. When someone else works on your code, they can learn the app’s architecture and will understand it better.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When Clean architecture came to the rescue ?

A

A strong base architecture is extremely important for an app to scale and meet the expectation of the user base.We got a task of replacement of API with new updated and optimized API structure. For integrating this kind of change made us kind of rewrite the whole app.

Why? Because the code was deeply coupled with response data models. At this time, I didn’t want to make the same mistakes over and over again. For resolving this problem, Clean architecture came to the rescue.

It is a bit pain in the starting but might be the best option for a large app with many features and SOLID approach. This architecture was proposed in 2012 by Robert C. Martin(Uncle Bob).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the benefits of the cleaner approach?

A

Benefits of the cleaner approach:

  1. Separation of code in different layers with assigned responsibilities making it easier for further modification.
  2. High level of abstraction
  3. Loose coupling between the code
  4. Testing of code is painless
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the Clean Architecture Layers?

A
  1. Domain layer: Would execute business logic which is independent of any layer and is just a pure kotlin package with no android specific dependency.
  2. Data layer: Would dispense the required data for the application to the domain layer by implementing interface exposed by the domain
  3. Presentation layer: Would include both domain and data layer and is android specific which executes the UI logic.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Domain Layer in Clean Architecture?

A

This will be the most generic layer of the three. It will connect the presentation layer with the data layer. This is the layer where app-related business logic will be executed.

UseCases

Use cases are the application logic executor. As the name depicts each functionality can have its separate use case. With more granularity of the use case creation, it can be reused more often.

Repositories

It specifies the functionalities required by the use cases which is implemented by the data layer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Data Layer in Clean Architecture?

A

This layer is responsible for providing the data required by the application. Data layer should be designed such data it can be re-used by any application without modification in their presentation logic.

API provides remote networking implementation. Any networking library can be integrated into this like retrofit, volley etc. Similarly, DB provides local database implementation.

In Repository, we have an implementation of the local, remote or any kind of data provider and above class NewsRepositoryImpl.kt implements the interface exposed by the domain layer. It acts as a single point of access to the data layer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Presentation Layer in Clean Architecture?

A

The presentation layer provides the UI implementation of the application. It is the dumb layer which only performs instruction with no logic in it. This layer internally implements architecture like MVC, MVP, MVVM, MVI etc. This is the layer where everything connects.

DI folder provides the injection all the dependencies at the start of an app like network related, View Models, Use Cases etc. DI in android can be implemented with dagger, kodein, koin or by just using the service locator pattern. It just depends upon the application like for complex app di can be pretty helpful.

Why using ViewModels?

As per the android documentation ViewModel:

Store and manage UI-related data in a lifecycle conscious way. It allows data to survive configuration changes such as screen rotations.

So, ViewModel retains the data on configuration change. In MVP, Presenter was bind to the view with the interface which makes it difficult to test but in ViewModel, there is no interface because of the architectural aware components.

A data wrapper class is used onto the LiveData as a helper class so that view gets to know about the status of the request i.e if it has been started, successful or any concerned state about the data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How all the layers are connected in Clean Architecture?

A

Each layer has its own entities which are specific to that package. Mapper is used for conversion of one layer entities to another. We are having different entities for each layer so that the layer becomes purely independent and only the required data gets passed to the subsequent layer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to implement Clean Architecture in Android ?

A

Project Structure

—————————-

Since Clean architecture can be applied anywhere, it’s important to know how you’ll implement it on Android.

You’ll split the project into two modules:

  • The existing app module.
  • A new core module that will hold all the code that doesn’t depend on Android SDK.

The following graph shows communication between the layers and how they are arranged in modules:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

An overview of Clean Architecture in combination with MVVM

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly