ရိုးရိုးလေး ရှင်းပြမယ်
Interfaces ကို ရိုးရိုးလေးနားလည်မယ်
Interface သည် class တစ်ခုက ဘာ methods/properties တွေရှိရမယ်ဆိုတာ သတ်မှတ်ပေးတဲ့ contract တစ်ခုပါ။ Interface ကို implement လုပ်တဲ့ class က interface ထဲက members တွေကို ရေးပေးရပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။
- Object / dependency
- C# type contract + CLR
- Reusable behavior
Interfaces ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
interface INotification
{
void Send(string message);
}
class EmailNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"Email sent: {message}");
}
}
class SmsNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"SMS sent: {message}");
}
}
class Program
{
static void Main(string[] args)
{
INotification notifier = new EmailNotification();
notifier.Send("Welcome to ThutaTech");
}
}Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Object / dependency
interface INotificationInterface သည် class တစ်ခုက ဘာ methods/properties တွေရှိရမယ်ဆိုတာ သတ်မှတ်ပေးတဲ့ contract တစ်ခုပါ။ Interface ကို implement လုပ်တဲ့ class က interface ထဲက members တွေကို ရေးပေးရပါတယ်။
- C# type contract + CLR
public void Send(string message)Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။
- Reusable behavior
}IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။
- Object / dependency
- C# type contract + CLR
- Reusable behavior
Success
Interfaces ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။
Interface သည် class တစ်ခုက ဘာ methods/properties တွေရှိရမယ်ဆိုတာ သတ်မှတ်ပေးတဲ့ contract တစ်ခုပါ။ Interface ကို implement လုပ်တဲ့ class က interface ထဲက members တွေကို ရေးပေးရပါတယ်။
interface INotification
{
void Send(string message);
}
class EmailNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"Email sent: {message}");
}
}
class SmsNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"SMS sent: {message}");
}
}
class Program
{
static void Main(string[] args)
{
INotification notifier = new EmailNotification();
notifier.Send("Welcome to ThutaTech");
}
}Interface အသုံးဝင်တဲ့နေရာ
Notification ကို email, SMS, push notification စတာတွေဖြင့်ပို့နိုင်ပေမယ့် app ရဲ့ main logic က Send() method တစ်ခုကိုပဲသိနေဖို့လိုပါတယ်။ Implementation ပြောင်းချင်ရင် class ပြောင်းလိုက်ရုံပါ။
Email sent: Welcome to ThutaTechReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
interface INotification
{
void Send(string message);
}
class EmailNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"Email sent: {message}");
}
}
class SmsNotification : INotification
{
public void Send(string message)
{
Console.WriteLine($"SMS sent: {message}");
}
}
class Program
{
static void Main(string[] args)
{
INotification notifier = new EmailNotification();
notifier.Send("Welcome to ThutaTech");
}
}ရလဒ်: Interfaces ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Object / dependency → C# type contract + CLR → Reusable behavior
- Interface သည်implementation မဟုတ်ဘဲcapability contract ဖြစ်ပြီး multiple types ကိုdecouple/test လုပ်နိုင်စေပါတယ်။ Interface ကိုconsumer need အလိုက်သေးငယ်စေပြီး versioning နဲ့behavioral expectations ကိုdocumentation/tests ဖြင့်ဖော်ပြပါ။
- Class တစ်ခု၏method အားလုံးကိုinterface ထဲcopy လုပ်ခြင်းကmeaningful abstraction မဖြစ်ပါ။
- IContactStore interface, in-memory implementation နဲ့fake test implementation ရေးပြီး UI/business logic ကိုstorage မှခွဲပါ။
Object-oriented programming in C# — Microsoft Learn