تصميم واجهة للقدرات
حدّد عقدًا مشتركًا لميزة خاصة بالمنصة.
تصميم واجهة للقدرات درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. 🎉
الأسئلة الشائعة
هل درس «تصميم واجهة للقدرات» مجاني؟
نعم — نص درس «تصميم واجهة للقدرات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «تصميم واجهة للقدرات»؟
حدّد عقدًا مشتركًا لميزة خاصة بالمنصة. تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تصميم واجهة للقدرات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.