singleとfactoryのScopes
シングルトンと新しいインスタンスのどちらを使うか選びます
「singleとfactoryのScopes」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 👍
よくある質問
「singleとfactoryのScopes」レッスンは無料ですか?
はい。「singleとfactoryのScopes」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「singleとfactoryのScopes」で何を学びますか?
シングルトンと新しいインスタンスのどちらを使うか選びます ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「singleとfactoryのScopes」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 共有Koin Moduleを宣言する
- singleとfactoryのScopes
- AndroidとiOSでKoinを起動する
- ViewModelsとReposにInjectする