ရိုးရိုးလေး ရှင်းပြမယ်
Protocols ကို ရိုးရိုးလေးနားလည်မယ်
Protocol က class, struct, enum တစ်ခုက ဘယ် property/method တွေရှိရမလဲဆိုတဲ့ contract ပါ။ Swift development မှာ protocol-oriented programming ကိုအများကြီးသုံးကြပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Protocol requirement ထဲက method/property တစ်ခုမရေးမိရင် conform မဖြစ်ဘဲ compile error ပြပါမယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Protocols ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Value / reference
- Protocol + type composition
- Reusable behavior
Protocols ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
protocol Payable {
var amount: Double { get }
func pay()
}
struct CreditCardPayment: Payable {
let amount: Double
func pay() {
print("Paid $\(amount) with credit card")
}
}
let payment = CreditCardPayment(amount: 29.99)
payment.pay()Protocols ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Value / reference
protocol Payable {Protocol က class, struct, enum တစ်ခုက ဘယ် property/method တွေရှိရမလဲဆိုတဲ့ contract ပါ။ Swift development မှာ protocol-oriented programming ကိုအများကြီးသုံးကြပါတယ်။
- Protocol + type composition
func pay() {Protocols ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Reusable behavior
payment.pay()Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Value / reference
- Protocol + type composition
- Reusable behavior
Success
Protocols ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Protocol requirement ထဲက method/property တစ်ခုမရေးမိရင် conform မဖြစ်ဘဲ compile error ပြပါမယ်။
Protocol က class, struct, enum တစ်ခုက ဘယ် property/method တွေရှိရမလဲဆိုတဲ့ contract ပါ။ Swift development မှာ protocol-oriented programming ကိုအများကြီးသုံးကြပါတယ်။
Protocol သုံးခြင်းက concrete type တစ်ခုတည်းနဲ့မချိတ်ဘဲ behavior ပေါ်မူတည်ပြီး code ရေးနိုင်စေပါတယ်။ Testing, dependency injection, reusable components တွေမှာအလွန်အသုံးဝင်ပါတယ်။
protocol Payable {
var amount: Double { get }
func pay()
}
struct CreditCardPayment: Payable {
let amount: Double
func pay() {
print("Paid $\(amount) with credit card")
}
}
let payment = CreditCardPayment(amount: 29.99)
payment.pay()Payable protocol က amount property နဲ့ pay() method ရှိရမယ်လို့သတ်မှတ်ပါတယ်။ CreditCardPayment struct က အဲဒီ contract ကိုလိုက်နာထားပါတယ်။
Paid $29.99 with credit cardReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
protocol Payable {
var amount: Double { get }
func pay()
}
struct CreditCardPayment: Payable {
let amount: Double
func pay() {
print("Paid $\(amount) with credit card")
}
}
let payment = CreditCardPayment(amount: 29.99)
payment.pay()ရလဒ်: Protocols ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Value / reference → Protocol + type composition → Reusable behavior
- Protocols ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Protocol requirement ထဲက method/property တစ်ခုမရေးမိရင် conform မဖြစ်ဘဲ compile error ပြပါမယ်။
- Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
The Swift Programming Language — Swift.org