๐Ÿ”ธuser

dev โŸฉ terms โŸฉ roles โŸฉ user

ไฝฟ็”จ่€…โ•ฑuserโ•ฑconsumerโ•ฑclient ็จ‹ๅผ้–‹็™ผไธญ๏ผŒไฝฟ็”จๅ‡ฝๆ•ธใ€ๅž‹ๅˆฅๅฎš็พฉ็š„ไบบ (ๆˆ–็จ‹ๅผ็ขผ)ใ€‚ ๐Ÿˆฏ ้€šๅธธๆŒ‡ใ€ŒๆŸไธ€ๆฎต็จ‹ๅผ็ขผใ€

// โญ๏ธ implementer: 
// ๅœจ้€™่ฃก๏ผŒ`makeCircle` ๅฐฑๆ˜ฏๅฏฆ็พ่€…๏ผŒไป–ๆฑบๅฎšไบ†่ฟ”ๅ›žๅ€ผ็š„ๅ…ท้ซ”ๅž‹ๅˆฅๆ˜ฏ `Circle`๏ผŒ
// ้›–็„ถๅฐๅค–ๅชๆšด้œฒ `some Shape`(ไฝฟ็”จ่€…ๅช็Ÿฅ้“่ฟ”ๅ›žๅ€ผๆ˜ฏ `some Shape`)ใ€‚
func makeCircle(radius: Double) -> some Shape {
    return Circle(radius: radius)
}

// โญ๏ธ user:
// ๅ‘ผๅซ `makeCircle` ็š„็จ‹ๅผ็ขผๅฐฑๆ˜ฏไฝฟ็”จ่€…๏ผŒไฝฟ็”จ่€…ๅช็Ÿฅ้“่ฟ”ๅ›žๅž‹ๅˆฅ็ฌฆๅˆ Shape ๅ”่ญฐ๏ผŒ
// ไฝ†ไธ็Ÿฅ้“ๅ…ท้ซ”ๅž‹ๅˆฅๆ˜ฏไป€้บผ๏ผˆๅ› ็‚บ่ฟ”ๅ›žๅ€ผๆ˜ฏ some Shape๏ผ‰ใ€‚
let shape = makeCircle(radius: 5)
// โญ๏ธ implementer: ๅฎš็พฉๅž‹ๅˆฅใ€ๅ‡ฝๆ•ธ
// ------------------------------
protocol MyShape {
    func area() -> Double
}

struct MyCircle: MyShape {
    var radius: Double
    func area() -> Double { .pi * radius * radius }
}

func makeCircle() -> some MyShape {
    return MyCircle(radius: 10)      // โญ๏ธ ๅฏฆ็พ่€…ๆฑบๅฎšๅ…ท้ซ”ๅž‹ๅˆฅ๏ผš`MyCircle`
}

// โญ๏ธ user: ไฝฟ็”จๅž‹ๅˆฅใ€ๅ‡ฝๆ•ธ
// ------------------------------
let shape = makeCircle()             // โญ๏ธ ไฝฟ็”จ่€…ๅช่ƒฝ็Ÿฅ้“ๅฎƒๆ˜ฏ๏ผš`some MyShape`
print(shape.area())                  // โญ๏ธ ๅฏไฝฟ็”จ `MyShape` ๅ”่ญฐ็š„ๆ–นๆณ•

Last updated