0Pricing
Kotlin Multiplatform Academy · Lekcja

Zakresy single a factory

Wybieranie singletonów zamiast nowych instancji

Zakresy single a factory to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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. 👍

Często zadawane pytania

Czy lekcja „Zakresy single a factory” jest bezpłatna?

Tak — pełny tekst „Zakresy single a factory” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Zakresy single a factory”?

Wybieranie singletonów zamiast nowych instancji Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?

Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Zakresy single a factory”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?

Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Deklarowanie współdzielonego modułu Koin
  2. Zakresy single a factory
  3. Uruchamianie Koin na Androidzie i iOS
  4. Wstrzykiwanie zależności do ViewModels i repozytoriów
← Powrót do Kotlin Multiplatform Academy