๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Kotlin] Companion Object์˜ ํ™œ์šฉ

medium.com/google-developer-experts/companion-objects-kotlins-most-unassuming-power-feature-fb5c0451fbd0

 

Companion Objects: Kotlin’s most unassuming power feature

In which I argue the case for Kotlin’s Companion Objects being the Clark Kent of language features, in being deceptively powerful, and how…

medium.com

Companion Object๊ฐ€ ๋‹จ์ˆœํžˆ ํด๋ž˜์Šค์˜ ์ •์  ๋ฉค๋ฒ„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ ์ด์ƒ์˜ ํ™œ์šฉ ๋ฐฉ๋ฒ•์ด ์žˆ์Œ์„ ์•Œ๋ ค์คŒ.

 

-> Companion Oject ์ •๋ฆฌ

 

 

** ์›๊ธ€์˜ ์˜ˆ์ œ ์ฐธ๊ณ  ํ•„์ˆ˜

 

1. API ํ™•์žฅ ์ง€์ 

 

Functional interface์— Compation Object๋ฅผ ์„ ์–ธํ•ด์„œ  extension์„ ๊ตฌํ˜„ํ•œ๋‹ค.

 

-> Functional interface ์ •๋ฆฌ

2. ํŒŒ์„œ/ํŒฉํ† ๋ฆฌ

 

๋‹ค๋ฅธ ํ˜•์‹์˜ ๋งˆ์ดํฌ๋กœ ํƒ€์ž…์˜ ์ƒ์„ฑ์ž๋ฅผ Compation object ์•ˆ์— ์“ธ ์ˆ˜ ์žˆ๋‹ค. 

 

3. ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์„ ๋‹ค์–‘ํ™”์‹œํ‚ด

 

4. Companion object๋„ ์ถ”์ถœ๊ณผ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•จ

 

superclass/interface์— ์ถ”๊ฐ€๋œ ํ™•์žฅํ•จ์ˆ˜๋„ ์ž๋™์œผ๋กœ Companion Obejct์— ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์„ ์ด์šฉ.