반응형
MainActivity.kt
package com.example.mysingletonpractice
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class MainActivity : AppCompatActivity() {
val TAG: String = "로그"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "MainActivity - onCreate() called")
val myNormalClass_1 = MyNormalClass()
val myNormalClass_2 = MyNormalClass()
Log.d(TAG, "MainActivity myNormalClass_1 : $myNormalClass_1")
Log.d(TAG, "MainActivity myNormalClass_2 : $myNormalClass_2")
val mySingleTonClass_1 = MySingletonClass
val mySingleTonClass_2 = MySingletonClass
Log.d(TAG, "MainActivity - val mySingleTonClass_1 = $mySingleTonClass_1")
Log.d(TAG, "MainActivity - val mySingleTonClass_2 = $mySingleTonClass_2")
val mySQLOpenHelper_1 = MySQLOpenHelper(this)
val mySQLOpenHelper_2 = MySQLOpenHelper(this)
Log.d(TAG, "MainActivity - mySQLOpenHelper_1 : $mySQLOpenHelper_1")
Log.d(TAG, "MainActivity - mySQLOpenHelper_2 : $mySQLOpenHelper_2")
val MySQLOpenHelperSingleton_1 = MySQLOpenHelperSingleton.getInstance(this)
val MySQLOpenHelperSingleton_2 = MySQLOpenHelperSingleton.getInstance(this)
Log.d(TAG, "MainActivity - MySQLOpenHelperSingleton_1 : $MySQLOpenHelperSingleton_1")
Log.d(TAG, "MainActivity - MySQLOpenHelperSingleton_2 : $MySQLOpenHelperSingleton_2")
}
}
MySQLOpenHelperSingleton
package com.example.mysingletonpractice
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class MySQLOpenHelperSingleton private constructor(context: Context) : SQLiteOpenHelper(context, "MyDB", null, 1) {
val TAG = "MySQLOpenHelperSingleton"
companion object{
// 자기 자신 변수 선언
@Volatile private var instance: MySQLOpenHelperSingleton? = null
// 자기 자신 가져오기
fun getInstance(context: Context) : MySQLOpenHelperSingleton =
instance ?: synchronized(this){
instance ?: MySQLOpenHelperSingleton(context).also{
instance = it
}
}
}
override fun onCreate(p0: SQLiteDatabase?) {
TODO("Not yet implemented")
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
TODO("Not yet implemented")
}
}
반응형
'Android' 카테고리의 다른 글
안드로이드 정리 (0) | 2022.10.20 |
---|---|
[안드로이드 & 코틀린] 레트로핏과 인터셉터를 이용하여 API 호출하기 (0) | 2022.10.02 |
[Android] lottie를 활용하여 좋아요 클릭 버튼 만들기 (0) | 2022.10.01 |
안드로이드 레이아웃 종류 LinearLayout, RelativeLayout, ConstraintLayout (0) | 2022.10.01 |
[Android & Kotlin] 프래그먼트에서 액티비티 호출하기 (2) | 2022.09.27 |