single vs factory Scopes
Choose singletons versus fresh instances.
single vs factory Scopes is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 👍
Frequently asked questions
Is the “single vs factory Scopes” lesson free?
Yes — the full text of “single vs factory Scopes” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “single vs factory Scopes”?
Choose singletons versus fresh instances. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “single vs factory Scopes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declare a Shared Koin Module
- single vs factory Scopes
- Start Koin on Android & iOS
- Inject into ViewModels & Repos