Android — Clean Architecture

Hari Prasad
3 min readAug 22, 2023

--

Clean Architecture to create maintainable, modular, and testable code by separating concerns into distinct layers and enforcing a clear dependency flow. This approach helps in building applications that are adaptable to changes, scalable, and easy to maintain.

  • Dependency flow — out to in

Layers in Clean Architecture

  • Entities (Core Business Logic)
    At the center of Clean Architecture are the entities, which represent the core business logic of the application.
    — They encapsulate the most fundamental concepts of the application’s domain.
    — Entities are independent of any external frameworks or libraries and focus solely on the business rules.
  • Use Cases (Interactors)
    — Use Cases contain the application-specific business logic.
    — They are responsible for orchestrating the flow of data between the entities, enforcing business rules, and performing use-case-specific operations.
    — Use Cases encapsulate the core business logic and ensure that it remains independent of external details.
  • Interfaces (Boundaries)
    — Interfaces define the contracts between the inner layers (entities and use cases) and the outer layers (UI and data sources).
    — This layer uses abstract interfaces to establish a clear separation between the core business logic and the outer layers, promoting the Dependency Inversion Principle.
    — This also enables flexibility and adaptability by allowing the inner layers to define what they need without specifying implementation details.
  • Frameworks and Drivers
    — The outermost layer consists of the frameworks, libraries, and tools that interact with the application.
    — This includes the user interface components (Activities, Fragments), database libraries (Room), network libraries (Retrofit), and any other platform-specific components.
    — The Frameworks and Drivers layer adapts the core business logic to the specific platform requirements.

Layers in Android

  • Presentation Layer — Activity/Fragment
    — Would include both domain and data layer and is android specific which executes the UI logic.
  • Data Layer UseCases
    — Would dispense the required data for the application to the domain layer by implementing interface exposed by the domain
  • Domine Layer Models, Repositories
    — Would execute business logic which is independent of any layer and is just a pure kotlin package with no android specific dependency.

User Interaction

  • The user interacts with the UI components (Activities/Fragments).
  • UI components communicate with the ViewModel (part of the Presentation Layer).

ViewModel and Use Cases

  • ViewModel receives user actions and transforms them into meaningful operations using Use Cases.
  • ViewModel might request data from the Use Cases to update the UI.

Use Cases and Repositories

  • Use Cases interact with repositories (implementations of interfaces defined in the domain layer).
  • Repositories handle data retrieval from various sources like databases, network APIs, etc.

Repositories and Entities

  • Repositories fetch data from different sources and transform them into entities (domain objects).
  • Entities encapsulate the data and business logic of the application.

Entities and Use Cases

  • Use Cases orchestrate the interaction between entities to perform specific operations.
  • Use Cases enforce business rules and implement application-specific logic.

Use Cases and ViewModel

  • Use Cases provide results back to the ViewModel, which in turn updates the UI.

Activities, Fragments
ViewModel
Use Cases
Repositories
Models

  • UI components (Activities, Fragments) interact with the ViewModel (part of the Presentation Layer) through the Android ViewModel class.
  • ViewModel acts as an intermediary between the UI and the Use Cases, exposing observable LiveData or StateFlow objects for UI updates.
  • Dependency Injection (e.g., Dagger or Hilt) is often used to provide dependencies (Use Cases, Repositories) to the ViewModel.
  • Repositories, which implement the interfaces defined in the Domain Layer, handle data retrieval and storage. They abstract the data source, whether it’s a local database, remote API, or any other source.
  • Models used in the UI layer are separate from the domain entities. Data transformations between models and entities occur in the Use Cases or Repositories.

--

--

No responses yet