Simplify ViewModelProvider.Factory() Implementation with Kotlin Lambda and Object Expressions
Tags: android, kotlin, beginners
Published: 2023-09-16 00:00:00

**Learn how to streamline the implementation of ViewModelProvider.Factory() using Kotlin Lambdas and Object Expressions with examples.**

If you are like me and still have not gotten used to the [Dependency Injection](https://vtsen.hashnode.dev/how-to-implement-hilt-in-android-app) framework (don't you think it complicates things?), you probably have been implementing the `ViewModelProvider.Factory()` interface for your view models that has custom constructor parameters.

## Multiple ViewModel Factory Classes

```kotlin
class AViewModelFactory(private val repository: ARepository) :
    ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return AViewModel(repository) as T
    }
}
```

For example, `AViewModelFactory` is the view model factory class that creates `ViewModel` which takes in `ARepository` as a constructor parameter. So what if you have another ViewModel to create? Then, you need to implement another factory class (i.e. `BViewModelFactory`)

```kotlin
class BViewModelFactory(private val repository: BRepository) :
    ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return BViewModel(repository) as T
    }
}
```

Well, this seems redundant and can be simplified.

## Kotlin Lambda and Object Expressions

You can simply the implementation without explicitly having these factory classes using Kotlin [Lambda Functions](https://vtsen.hashnode.dev/complete-c-to-kotlin-syntax-comparisons#heading-lambda-methods-functions) and [Object Expressions](https://vtsen.hashnode.dev/complete-c-to-kotlin-syntax-comparisons#heading-static-vs-object-keyword) as you can see in the following `createViewModelFactory()` helper function.

```kotlin
fun <VM : ViewModel> createViewModelFactory(createViewModel: () -> VM)
    : ViewModelProvider.Factory {

    return object : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return createViewModel() as T
        }
    }
}
```

The `createViewModel` is the lambda where the callers define how they want to create the ViewModel. The return of this function is the implementation of `ViewModelProvider.Factory()` interface for the ViewModel that you want to create.

So, instead of creating the ViewModel like this:

```kotlin
@Composable()
fun WithoutHelperFunctions() {
    val aViewModel: AViewModel = viewModel(
        factory = AViewModelFactory(ARepository()),
    )

    val bViewModel: AViewModel = viewModel(
        factory = BViewModelFactory(BRepository()),
    )
}
```

You can create the ViewModel with this `createViewModelFactory()` helper function:

```kotlin
@Composable()
fun WithHelperFunctions() {
    val aViewModel: AViewModel = viewModel(
        factory = createViewModelFactory {
            AViewModel(ARepository())
        }
    )

    val bViewModel: BViewModel = viewModel(
        factory = createViewModelFactory {
            BViewModel(BRepository())
        }
    )
}
```

The benefit is, you no longer need to declare multiple factory classes. You just need to have one `createViewModelFactory()` helper function.

## ViewModel Creation Articles

If you're not familiar with ViewModel creations, you may want to visit my following blog posts:

- [Recommended Ways To Create ViewModel or AndroidViewModel](https://vtsen.hashnode.dev/recommended-ways-to-create-viewmodel-or-androidviewmodel)
- [Implement ViewModelProvider.Factory Interface for Custom ViewModel / AndroidViewModel Factory Creation](https://vtsen.hashnode.dev/implement-viewmodelproviderfactory-interface-for-custom-viewmodel-androidviewmodel-factory-creation)
- [View Model Creation in Jetpack Compose](https://vtsen.hashnode.dev/view-model-creation-in-jetpack-compose)

## Source Code

GitHub Repository: [Demo\_ViewModelFactory](https://github.com/vinchamp77/Demo_ViewModelFactory)

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/simplify-viewmodelproviderfactory-implementation-with-kotlin-lambda-and-object-expressions)*.