Eine Capability-Schnittstelle entwerfen
Definieren Sie einen gemeinsamen Vertrag für eine Plattformfunktion.
Eine Capability-Schnittstelle entwerfen ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Some Things Are Platform-Only
Reading the battery level or device model is different on Android and iOS. Shared code still needs a clean way to ask for it. 🔋
Start With the Need, Not the API
Design from what your app actually needs. A simple interface states the capability in plain Kotlin, hiding every platform detail.
interface BatteryInfo {
fun level(): Int
}One Contract, Many Backers
The interface is a promise: anyone who implements it must supply a real battery level. Each platform fulfills that promise its own way.
Keep Methods Tiny
Expose only what callers need. A focused method like level() is far easier to implement per platform than a sprawling, do-everything interface.
Name It for the Domain
Call it BatteryInfo, not AndroidBatteryHelper. A good capability name describes the feature, never the platform behind it.
Return Plain Types
Hand back simple Kotlin types like Int or String. Plain return values keep the interface portable and free of platform classes.
fun model(): StringGroup Related Calls
If several calls belong together, gather them in one interface. Device facts like model and OS version fit naturally side by side.
interface DeviceInfo {
fun model(): String
fun osVersion(): String
}Shared Code Depends on the Type
Your common logic talks only to the interface, never to a concrete class. That keeps business rules testable and platform-agnostic.
class Banner(val info: DeviceInfo)Fakes Become Easy
Because callers depend on the interface, tests can pass a fake that returns canned values. No emulator or real device required. ✅
class FakeDeviceInfo : DeviceInfo {
override fun model() = "Pixel"
override fun osVersion() = "14"
}Avoid Leaking Platform Types
Never put a UIDevice or Android Context into the interface signature. The moment you do, shared code stops compiling on the other target.
Implementations Come Later
The contract is step one. Each platform will supply its own implementation of this interface, which you wire in separately as the next step.
Quick Check
Think about what belongs in the capability contract.
Recap
You learned to design a small, domain-named interface that states a capability, returns plain types, and keeps shared code portable and testable. 🎉
Häufig gestellte Fragen
Ist die Lektion „Eine Capability-Schnittstelle entwerfen“ kostenlos?
Ja — der vollständige Text von „Eine Capability-Schnittstelle entwerfen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Eine Capability-Schnittstelle entwerfen“?
Definieren Sie einen gemeinsamen Vertrag für eine Plattformfunktion. Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Eine Capability-Schnittstelle entwerfen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Eine Capability-Schnittstelle entwerfen
- expect/actual für Geräteinformationen
- Dateisystem und Pfade je Plattform
- Plattformimplementierungen injizieren