0Pricing
Kotlin Multiplatform Academy · Aula

Scopes single versus factory

Escolha entre instâncias únicas e instâncias novas

Scopes single versus factory é uma aula grátis de Kotlin Multiplatform Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Kotlin Multiplatform Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Kotlin Multiplatform Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Two Ways to Build

Koin gives you two main definition keywords: single and factory. They differ in one thing, how often Koin builds the object.

single Means One Instance

A single is created once and shared everywhere. Every part of your app that asks for it receives the very same object. 🔁

single { UserRepository(get()) }

factory Means Fresh Each Time

A factory builds a brand new instance on every request. Nothing is cached, so two callers never share the same object.

factory { ProfileUseCase(get()) }

When to Pick single

Use single for things that hold state or are expensive to build, like a repository, an HTTP client, or a database driver.

When to Pick factory

Reach for factory when each caller wants its own short-lived object, such as a stateless use case or a one-shot mapper.

single Is Lazy by Default

A single is not built when you declare it. Koin creates it the first time something actually asks for it, then keeps it around.

Force Eager Creation

Need a single ready at startup instead of on first use? Add createdAtStart so Koin builds it the moment the container starts.

single(createdAtStart = true) { Analytics() }

Mixing Them in a Graph

A single repository can depend on a factory mapper. Koin happily mixes scopes, building the factory fresh inside the long-lived single.

Watch Out for Shared State

If you make something single by mistake, callers may share state you meant to keep private, a classic source of subtle multiplatform bugs.

A Practical Module

Here a long-lived repository and a fresh use case live side by side, each scoped to match how it is used.

module {
    single { UserRepository(get()) }
    factory { LoginUseCase(get()) }
}

Default to single

When unsure, start with single. Most app-wide services want one instance, and you can switch to factory the moment you truly need fresh objects.

Quick Check

Pick the right scope behavior.

Recap

You learned that single shares one instance while factory builds a new one each time. Pick single for shared services, factory for short-lived helpers. 👍

Perguntas Frequentes

A aula “Scopes single versus factory” é grátis?

Sim — o texto completo de “Scopes single versus factory” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Kotlin Multiplatform Academy, atualize para CoddyKit PRO. O curso de Kotlin Multiplatform Academy inclui 4 aulas no total.

O que vou aprender em “Scopes single versus factory”?

Escolha entre instâncias únicas e instâncias novas Você pratica Kotlin Multiplatform Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Kotlin Multiplatform Academy?

Nenhuma experiência prévia é necessária. Kotlin Multiplatform Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Scopes single versus factory”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Kotlin Multiplatform Academy?

Sim. Cada aula de Kotlin Multiplatform Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Declare um módulo Koin compartilhado
  2. Scopes single versus factory
  3. Inicie o Koin no Android e no iOS
  4. Injete em ViewModels e repositórios
← Voltar para Kotlin Multiplatform Academy