Chapter10: Architecture Patterns

Architecture Patterns! This is the chapter that turns your app from “working code” into professional, maintainable, testable code that can grow to thousands of lines without becoming a nightmare. In January 2026, the Android community has settled on a very clear recommended stack for most apps:

  • MVVM (Model-View-ViewModel) as the primary UI pattern (especially with Jetpack Compose).
  • Clean Architecture (or layered architecture) for separation of concerns.
  • Repository pattern to abstract data sources.
  • Hilt for dependency injection (latest stable is 2.59 released Jan 21, 2026 — fully supports AGP 9.0+ and KSP).

We’ll go deep like we’re refactoring your Tip Calculator / To-Do / Posts app together in Airoli — explaining why each pattern exists, common mistakes, and a full hands-on refactor using clean architecture + MVVM + Hilt + Repository.

1. MVVM (Model-View-ViewModel) – The UI Layer Pattern

Why MVVM in 2026?

  • View (Compose UI) only displays and collects user events.
  • ViewModel holds UI state & business logic, survives config changes.
  • Model = data layer (repository, use cases).
  • Unidirectional data flow: View → events → ViewModel → state → View.
  • Testable: ViewModel has no Android context → easy unit tests.

Core components:

  • View → Composable functions.
  • ViewModel → @HiltViewModel class with StateFlow / LiveData for UI state.
  • UiState → Sealed/data class holding loading/error/success states.

Example UiState:

Kotlin

ViewModel:

Kotlin

Composable observes: val state by viewModel.uiState.collectAsStateWithLifecycle()

2. Dependency Injection with Hilt Basics

Why Hilt?

  • Reduces boilerplate vs manual DI.
  • Provides @HiltViewModel, @Inject constructors.
  • Scopes: @Singleton, @ActivityScoped, etc.
  • Integrates with Navigation, WorkManager, etc.

Setup (latest 2026 style – using KSP instead of KAPT)

In root build.gradle.kts:

Kotlin

In app/build.gradle.kts:

Kotlin

Application class:

Kotlin

In AndroidManifest.xml:

XML

Now you can @Inject almost anything!

3. Repository Pattern + Clean Architecture Layers

Clean Architecture layers (from inner to outer):

  1. Domain (core business logic – pure Kotlin, no Android deps)
    • Entities (data classes like Post)
    • Use Cases / Interactors (e.g., GetPostsUseCase)
    • Repositories (interfaces)
  2. Data (implementation of data sources)
    • Remote (Retrofit)
    • Local (Room/DataStore)
    • Repository impl
  3. Presentation (UI + ViewModel)
    • Composables
    • ViewModels

Repository → Single source of truth, abstracts where data comes from (API, DB, cache).

Example:

Kotlin

Use Case (domain layer):

Kotlin

4. Hands-on: Refactor Your Posts App with Clean Architecture

Let’s refactor the JSONPlaceholder Posts app from Chapter 9 into clean MVVM + Hilt + Repository.

Project Structure (recommended for 2026):

text

Step-by-step refactor:

  1. Create Hilt Module (di/AppModule.kt)
Kotlin
  1. Move to clean layers (create folders as above)
  • domain/model/Post.kt (same data class)
  • domain/repository/PostRepository.kt (interface)
  • domain/usecase/GetPostsUseCase.kt (as above)
  • data/repository/PostRepositoryImpl.kt (impl with Retrofit)
  • presentation/viewmodel/PostsViewModel.kt (as MVVM example above, @HiltViewModel)
  • presentation/screen/PostsScreen.kt (Composable with state collection)
  1. Update PostsScreen to use injected ViewModel:
Kotlin
  1. Add to NavHost (if multi-screen):
Kotlin

Benefits you get after refactor:

  • ViewModel has no direct Retrofit – easy to mock for tests.
  • Change API to Room/offline → only change RepositoryImpl.
  • Add caching, error handling, mapping → in UseCase/Repo.
  • Scalable: Add feature modules later.

Test tip: Write unit test for UseCase/ViewModel (no Android deps):

Kotlin

You’ve now refactored to clean, modern Android architecture! This structure is what most serious 2026 apps (news, e-commerce, social) use.

Questions? Want to add Room caching to the repo? Feature module? Testing setup? Or refactor your Tip Calculator instead? Tell me — next chapter: Advanced topics (permissions, background, testing, publishing). You’re at pro level now — incredible journey! Keep it up! 🚀🏗️

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *