Android with Kotlin

Single Tone

JinTonix 2023. 3. 1. 22:52

Single Tone 선언

class CoinService private constructor() {

    companion object {
        private var instance: CoinService? = null
        private lateinit var context: Context

        fun getInstance(_context: Context): CoinService {
            return instance ?: synchronized(this) {
                instance ?: CoinService().also {
                    context = _context
                    instance = it
                }
            }
        }
    }

    fun Test(){
        Log.d("Coin","Test 1")
    }
}

 

Single Tone 사용

val coinService = CoinService.getInstance(this)
coinService.Test()

 

'Android with Kotlin' 카테고리의 다른 글

CallBack 사용법  (0) 2023.03.19
파일 읽고 쓰기  (0) 2023.03.19
Gson 파일 읽고 쓰기 - Havah  (0) 2023.03.19
UpBit 실시간 코인 시세 확  (0) 2023.03.19
ByBit 실시간 거래 값 가져오기  (0) 2023.03.19