Thuta Learning
Swift
AdvancedProgrammingအခြေခံ

Protocols

Swiftသင်ခန်းစာ 2225 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Protocols ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
  • Nil, error, boundary နဲ့concurrency cases စမ်းနိုင်ရန်
  • Safe, testable Swift code ရေးနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 ကိုလျော့ပါ။

Mental Model
  1. Value / reference
  2. Protocol + type composition
  3. Reusable behavior

Protocols ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

swift
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 ကို အဆင့်လိုက်ဖတ်မယ်

  1. Value / referenceprotocol Payable {

    Protocol က class, struct, enum တစ်ခုက ဘယ် property/method တွေရှိရမလဲဆိုတဲ့ contract ပါ။ Swift development မှာ protocol-oriented programming ကိုအများကြီးသုံးကြပါတယ်။

  2. Protocol + type compositionfunc pay() {

    Protocols ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။

  3. Reusable behaviorpayment.pay()

    Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

Execution Flow
  1. Value / reference
  2. Protocol + type composition
  3. 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 တွေမှာအလွန်အသုံးဝင်ပါတယ်။

swift
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 ကိုလိုက်နာထားပါတယ်။

You should see
Paid $29.99 with credit card

Real-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 ကိုခန့်မှန်းပါ
swift
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

Protocols ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Protocol requirement ထဲက method/property တစ်ခုမရေးမိရင် conform မဖြစ်ဘဲ compile error ပြပါမယ်။
  • Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
  • Force unwrap သို့unchecked shared mutable state ဖြင့်compiler safety ကိုကျော်ခြင်း။

လက်တွေ့လေ့ကျင့်ခန်း

Protocols example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: Paid $29.99 with credit card

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Protocols | Thuta Learning