Repository Pattern
Separate data access from UI with the Repository pattern. Coordinate between Room database and network API as single sources of truth.
Why a Repository?
As your app grows, the ViewModel can become cluttered with data-access code (Room queries, Retrofit calls, SharedPreferences reads).
The Repository pattern moves all data access behind a single class. The ViewModel only talks to the Repository — it doesn't care where the data comes from.
The Architecture Layers
The recommended Android architecture has three layers:
- UI Layer — Activity/Fragment shows data, sends user events
- ViewModel — holds state, processes events, calls Repository
- Repository — fetches data from Room (local) or Retrofit (remote), decides which source to use