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

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

[Kotlin] Obejct๋กœ ์„ ์–ธํ•œ Singleton ๊ฐ์ฒด์˜ ์ƒ์„ฑ์€ Lazy ํ•œ๊ฐ€?

Java๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ Kotlin์œผ๋กœ ์ž‘์„ฑํ•˜๋‹ค๊ฐ€ ๋“  ์˜๋ฌธ์œผ๋กœ ์‹œ์ž‘ํ•จ.

Java์—์„œ Singleton ํŒจํ„ด์œผ๋กœ ์ž‘์„ฑ๋œ ํด๋ž˜์Šค๋“ค์€, ํ˜ธ์ถœ ์‹œ instance๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , instatnce๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์„ ์ž์ฃผ ๋ด์™”๋‹ค. Kotlin์—์„œ object๋กœ ์„ ์–ธํ•ด ์ž‘์„ฑํ•˜๋ฉด singleton ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๊ณ  ์•Œ๊ณ  ์žˆ์–ด์„œ, ์ •๋ง ํŽธ๋ฆฌํ•˜๊ฒŒ object ํ‚ค์›Œ๋“œ๋ฅผ ์“ฐ๋ฉฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, java ์ฝ”๋“œ์—์„œ ์ž‘์„ฑํ•œ singleton ํŒจํ„ด๊ณผ ์ •๋ง ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋Š”๊ฒŒ ๋งž๊ฒ ์ง€?๋ผ๋Š” ์˜๋ฌธ์ด ๋“ค์—ˆ๋‹ค. 

ํ™•์ธ ๋“ค์–ด๊ฐ„๋‹ค.

 

๊ฒฐ๋ก  > ๊ณต์‹๋ฌธ์„œ ์ƒ์—  ๋”ฐ๋ฅด๋ฉด ๊ทธ๋Ÿฌํ•˜๋‹ค.

 

๊ณต์‹๋ฌธ์„œ์˜ ์–ธ๊ธ‰์„ ์ฐพ์•„๋ณธ๋‹ค.

https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview

 

Object expressions and declarations | Kotlin

 

kotlinlang.org

 

The initialization of an object declaration is thread-safe and done on first access.

๋ฐœ๋ฒˆ์—ญ

object ์„ ์–ธ์˜ ์ดˆ๊ธฐํ™”๋Š” thread-safeํ•˜๊ณ  ์ฒ˜์Œ ์ ‘๊ทผํ•  ๋•Œ์— ์ด๋ค„์ง‘๋‹ˆ๋‹ค.

 

done on first access ์ฒ˜์Œ ์ ‘๊ทผํ•  ๋•Œ์— ์ดˆ๊ธฐํ™”๊ฐ€ ์ด๋ค„์ง„๋‹ค๊ณ  ํ•˜๋‹ˆ, Java์—์„œ singleton ํŒจํ„ด์œผ๋กœ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค๊ฐ€ ์ฒ˜์Œ ํ˜ธ์ถœ๋  ๋•Œ instance์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•˜๊ณ  ๋‚˜์„œ ํ•„์š”์‹œ instance๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋กœ์ง๊ณผ ๋™์ผํ•œ ๋“ฏํ•˜๋‹ค.

 

๋‹ค๋ฅธ ๋ฌธ์„œ์—์„œ ์–ธ๊ธ‰ํ•œ ๊ธ€๋„ ์ฐพ์•˜๋‹ค.

https://in-kotlin.com/design-patterns/singleton/#Object_Companion_Object

 

In Kotlin - Singleton Class / Object / Companion Object

Learn how to use the singleton object in Kotlin. Check out usages in Android apps and dependency injection frameworks.

in-kotlin.com

The Kotlin language provides some features called Object or in combination with a class Companion Object. These declarations are designed to implement the Singleton class in Kotlin in a native way. One thing to mention is that objects are constructed in a lazy way. Also, they are never destroyed as they are available for the lifetime of the application.

๋ฐœ๋ฒˆ์—ญ

Kotlin ์–ธ์–ด๋Š” Object๋‚˜ ํด๋ž˜์Šค Companion Object ์™€ ๊ฒฐํ•ฉ๋œ ์ผ๋ถ€ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ ์–ธ๋“ค์€ Kotlin์—์„œ ๊ธฐ๋ณธ ๋ฐฉ์‹์œผ๋กœ Singleton ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ์„ค๊ณ„๋์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ์–ธ๊ธ‰ํ•  ๊ฒƒ์€, ๊ฐ์ฒด๋“ค์ด lazyํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ์•ฑ์˜ ์ˆ˜๋ช…๋™์•ˆ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์ ˆ๋Œ€ ํŒŒ๊ดด๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์š” ๋ฌธ์„œ ์ž‘์„ฑ์ž๋Š” ๋‚˜์ฒ˜๋Ÿผ ์˜๋ฌธ์„ ๊ฐ€์งˆ ์‚ฌ๋žŒ๋“ค์ด ๋ถ„๋ช… ์žˆ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‚˜๋ณด๋‹ค. 

 

Object๋กœ ์„ ์–ธํ•œ singleton ๊ฐ์ฒด์˜ decompile ์ฝ”๋“œ๋„ ๊ถ๊ธˆํ–ˆ๋Š”๋ฐ, ์ฐพ์•˜๋‹ค.

https://gist.github.com/yenerm/3e4795f9d5b6685b96a7dc4e3de5ea15

 

Kotlin object decompiled to Java

Kotlin object decompiled to Java. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

decompile ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ, ๊ฒฐ๊ตญ์—” Java๋กœ ์ž‘์„ฑํ•˜๋Š” Singleton ํŒจํ„ด ์ฝ”๋“œ๊ฐ€ ๋ณด์ธ๋‹ค.

 


Kotlin์˜ singleton ์„ ์–ธ, object์— ๋Œ€ํ•ด ๊ฒ€์ƒ‰ํ•˜๋ฉด์„œ, Kotlin์˜ object ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋งŒ๋“  ๊ฐ์ฒด๊ฐ€ synchronizedํ•œ์ง€, thread safeํ•œ์ง€ ์˜๋ฌธ์„ ๊ฐ–๋Š” ๊ฐœ๋ฐœ์ž๋“ค์ด ์žˆ์—ˆ๋‹ค๋Š” ๊ฒƒ๋„ ํ™•์ธํ–ˆ๋‹ค. ๋‹ค๋“ค ๋น„์Šทํ•œ ๊ณ ๋ฏผ๊ณผ ์ƒ๊ฐ, ์˜๋ฌธ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ตฌ๋‚˜ ์‹ถ๋‹ค๐Ÿ˜€

 

https://stackoverflow.com/questions/37386116/are-kotlin-object-singletons-methods-synchronized

 

Are Kotlin object/singleton's methods synchronized?

Does it make any sense to add the @Synchronized annotation to an object's methods to make them atomic or it isn't needed? For example: object Singleton { var field1: Int = 0 private ...

stackoverflow.com

https://stackoverflow.com/questions/30179793/are-kotlins-singletons-thread-safe

 

Are Kotlin's singletons thread safe?

Are Kotlin singletons (more specifically, object declarations) thread-safe by construction? If not, what is the best practice to write thread safe singletons in Kotlin? I would guess they are, but I

stackoverflow.com