ฉีดการใช้งานของแพลตฟอร์ม
จัดเตรียม actual ที่เหมาะสมผ่าน Koin หรือโรงงาน
ฉีดการใช้งานของแพลตฟอร์ม เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Who Supplies the Real Thing
An interface defines a capability, but something must hand shared code the real platform implementation. That wiring is dependency injection.
Avoid new in Shared Code
Shared logic should not construct platform classes itself. Instead it receives the implementation from outside, keeping it decoupled and testable.
Inject via Constructor
The simplest approach is a constructor parameter. Pass the capability in, and the class never knows which platform built it.
class Profile(private val info: DeviceInfo)Provide the Android Instance
On Android, the app module creates the real actual and passes it down when it builds the shared object.
val profile = Profile(DeviceInfo())Provide the iOS Instance
On iOS, Swift constructs the framework's DeviceInfo and injects it the same way, so shared code stays identical across platforms.
let profile = Profile(info: DeviceInfo())Scale Up With Koin
For many dependencies, a DI library like Koin declares each capability once in a shared module and resolves it on demand.
val appModule = module {
single<DeviceInfo> { DeviceInfo() }
}Bind Interface to Actual
In Koin you bind the interface type to the platform implementation, so callers ask for DeviceInfo and get the right concrete one.
single<Storage> { PlatformStorage() }Resolve Where You Need It
Anywhere in shared code you can pull a dependency from the graph by its type, no manual construction required.
val info: DeviceInfo by inject()Swap Fakes in Tests
Because everything is injected, a test simply provides a fake implementation. No platform, emulator, or device is involved. ✅
Profile(FakeDeviceInfo())One Wiring Point per App
Keep injection at each app's entry point. That single setup spot decides which actuals run, while shared code stays blissfully unaware.
Factories for Cheap Cases
If a DI library feels heavy, a plain factory function works too. It builds the platform object in one place and hands it to shared code.
fun deviceInfo(): DeviceInfo = DeviceInfo()Quick Check
Think about why shared code receives its dependencies.
Recap
You learned to inject platform implementations via the constructor or Koin, binding interfaces to actuals so shared code stays clean and testable. 🎉
คำถามที่พบบ่อย
บทเรียน “ฉีดการใช้งานของแพลตฟอร์ม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ฉีดการใช้งานของแพลตฟอร์ม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ฉีดการใช้งานของแพลตฟอร์ม”
จัดเตรียม actual ที่เหมาะสมผ่าน Koin หรือโรงงาน คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ฉีดการใช้งานของแพลตฟอร์ม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ออกแบบอินเทอร์เฟซความสามารถ
- expect/actual สำหรับข้อมูลอุปกรณ์
- ระบบไฟล์และเส้นทางแยกตามแพลตฟอร์ม
- ฉีดการใช้งานของแพลตฟอร์ม