Scope single e factory
Scegliere tra singleton e nuove istanze
Scope single e factory è una lezione Kotlin Multiplatform Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Kotlin Multiplatform Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 👍
Domande Frequenti
La lezione «Scope single e factory» è gratuita?
Sì — il testo completo di «Scope single e factory» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Kotlin Multiplatform Academy, passa a CoddyKit PRO. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.
Cosa imparerò in «Scope single e factory»?
Scegliere tra singleton e nuove istanze Eserciti Kotlin Multiplatform Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Kotlin Multiplatform Academy?
Non è richiesta alcuna esperienza precedente. Kotlin Multiplatform Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «Scope single e factory»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Kotlin Multiplatform Academy?
Sì. Ogni lezione Kotlin Multiplatform Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Dichiarare un modulo Koin condiviso
- Scope single e factory
- Avviare Koin su Android e iOS
- Iniettare dipendenze in ViewModel e Repository