Zaprojektuj interfejs możliwości
Zdefiniuj współdzielony kontrakt dla funkcji platformy.
Zaprojektuj interfejs możliwości to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 1 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.
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. 🎉
Często zadawane pytania
Czy lekcja „Zaprojektuj interfejs możliwości” jest bezpłatna?
Tak — pełny tekst „Zaprojektuj interfejs możliwości” 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 „Zaprojektuj interfejs możliwości”?
Zdefiniuj współdzielony kontrakt dla funkcji platformy. Ć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 1 z 4.
Ile czasu zajmuje lekcja „Zaprojektuj interfejs możliwości”?
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
- Zaprojektuj interfejs możliwości
- expect/actual dla informacji o urządzeniu
- System plików i ścieżki zależnie od platformy
- Wstrzykiwanie implementacji platformowych