Contents


Models

There are lots of definitions of the word Model. We will follow this description: domain model.

In practice, model classes in fore (whether they are explicitly named “Model” or not) will mostly contain data and/or logic, they will very likely pass off tasks like network or database access to sub layers, making good use of composition. They typically employ dependency injection techniques to do that, preferably via their constructors.

Model classes manage their own state and expose it via quick returning getter methods or properties (usually called by thin views), it’s the state of these models that is the single source of truth for the app, and something that can be persisted to maintain state across process death. The value of this state is also the main focus for unit tests.

When the model’s state changes, it’s the model’s responsibility to call notifyObservers() after that change, this will inform any observers that it’s now time to re-query the model to get the current state (often to redraw a UI).

“It’s the model’s responsibility to call notifyObservers()

An important thing about these models is that none of the code should know anything about View layer classes. The models are concerned with their data, their logic and their state, and that is all. They don’t know or care what interrogates their state via their getter methods - and this makes our Models extremely easy to test.

“The models are concerned with their data, their logic and their state, and that is all”

In the sample apps, the models are all found in the feature package, in a clean architecture app they will typically live in the domain module.

Here’s the code for a very simple model which represents a user’s Wallet

Writing a Basic Model

If you write a good model, using it in the rest of your app should be a piece of cake.

You’ll see that in all the sample apps, the models have been written with the assumption that all the methods are being accessed on a single thread (which for a live app would be the UI thread). Not having to worry about thread safety here is a very big win in terms of code complexity. The models can use threads and coroutines internally of course.

If you need to hop onto another thread for IO or any heavy processing, do it explicitly with something like an AsyncBuilder or launch a coroutine for example, and then pop back on to the UI thread when you are ready to update your state.

pop back on to the UI thread when you are ready to update your state

Check out a [few] [examples] from the sample apps, or if you’re already comfortable writing model code (most of this advice is generic and applies to writing ViewModels too, so it’s all fairly obvious) feel free to skim over the checklist below for a refresher and you should be good to go.

Model Checklist

For reference here’s a check list of recommendations for the model classes, as used in fore. Once you’ve had a go at writing one you can come back here to double check you have everything down: