Scopes single frente a factory
Elija entre singletons e instancias nuevas
Scopes single frente a factory es una lección gratuita de Kotlin Multiplatform Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Kotlin Multiplatform Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en 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. 👍
Preguntas frecuentes
¿La lección «Scopes single frente a factory» es gratis?
Sí — el texto completo de «Scopes single frente a factory» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Kotlin Multiplatform Academy, actualiza a CoddyKit PRO. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Scopes single frente a factory»?
Elija entre singletons e instancias nuevas Practicas Kotlin Multiplatform Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Kotlin Multiplatform Academy?
No se requiere experiencia previa. Kotlin Multiplatform Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «Scopes single frente a factory»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Kotlin Multiplatform Academy?
Sí. Cada lección de Kotlin Multiplatform Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Declare un módulo Koin compartido
- Scopes single frente a factory
- Inicie Koin en Android e iOS
- Inyecte dependencias en ViewModels y repositorios