0Pricing
Kotlin Multiplatform Academy · บทเรียน

ขอบเขต single เทียบกับ factory

เลือกใช้ซิงเกิลตันหรืออินสแตนซ์ใหม่

ขอบเขต single เทียบกับ factory เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ขอบเขต single เทียบกับ factory” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ขอบเขต single เทียบกับ factory”

เลือกใช้ซิงเกิลตันหรืออินสแตนซ์ใหม่ คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ขอบเขต single เทียบกับ factory” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ประกาศโมดูล Koin แบบ shared
  2. ขอบเขต single เทียบกับ factory
  3. เริ่มต้น Koin บน Android และ iOS
  4. ฉีดการพึ่งพาเข้า ViewModels และรีโพ
← กลับไปที่ Kotlin Multiplatform Academy