[안드로이드 스튜디오/코틀린] 싱글톤/싱글턴 패턴
MainActivity.kt
package com.shinyelee.android_study2
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
// 메인화면
class MainActivity : AppCompatActivity() {
val TAG: String = "로그"
// 뷰 생성
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// 그릴 xml 뷰 파일을 연결(설정) 시켜줌
setContentView(R.layout.activity_main)
Log.d(TAG, "MainActivity - onCreate() called")
// 일반 클래스
val myNormalClass1 = MyNormalClass()
val myNormalClass2 = MyNormalClass()
// 메모리 2개
Log.d(TAG, "MainActivity - myNormalClass1 : $myNormalClass1")
Log.d(TAG, "MainActivity - myNormalClass2 : $myNormalClass2")
// 싱글턴 클래스
val mySingletonClass1 = MySingletonClass
val mySingletonClass2 = MySingletonClass
// 메모리 1개
Log.d(TAG, "MainActivity - mySingletonClass1 : $mySingletonClass1")
Log.d(TAG, "MainActivity - mySingletonClass2 : $mySingletonClass2")
// 매개변수 있는 클래스
val mySQLOpenHelper1 = MySQLOpenHelper(this)
val mySQLOpenHelper2 = MySQLOpenHelper(this)
// 메모리 2개
Log.d(TAG, "MainActivity - mySQLOpenHelper1 : $mySQLOpenHelper1")
Log.d(TAG, "MainActivity - mySQLOpenHelper2 : $mySQLOpenHelper2")
// 매개변수 있는 클래스를 싱글턴으로
val mySQLOpenHelperSingleton1 = MySQLOpenHelperSingleton.getInstance(this)
val mySQLOpenHelperSingleton2 = MySQLOpenHelperSingleton.getInstance(this)
// 메모리 2개
Log.d(TAG, "MainActivity - mySQLOpenHelperSingleton1 : $mySQLOpenHelperSingleton1")
Log.d(TAG, "MainActivity - mySQLOpenHelperSingleton2 : $mySQLOpenHelperSingleton2")
}
}
MyNormalClass.kt
package com.shinyelee.android_study2
class MyNormalClass {
init {
}
}
MySingletonClass.kt
package com.shinyelee.android_study2
object MySingletonClass {
}
MySQLOpenHelper.kt
package com.shinyelee.android_study2
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class MySQLOpenHelper(context: Context): SQLiteOpenHelper(context, "MyDB", null, 1) {
val TAG: String = "로그"
override fun onCreate(p0: SQLiteDatabase?) {
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
}
}
MySQLOpenHelperSingleton.kt
package com.shinyelee.android_study2
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: String = "로그"
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?) {
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
}
}
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio] (Vertical) Scroll View, Horizontal Scroll View, Nested Scroll View (0) | 2022.04.29 |
---|---|
[Android Studio] Font Setting (0) | 2022.04.21 |
[Android Studio/Kotlin] Custom Dialog, Interface (0) | 2022.04.20 |
[Android Studio/Kotlin] Listener, Method, Click Event (0) | 2022.04.18 |
[Android Studio/Kotlin] Linear Layout, Relative Layout, Constraint Layout (0) | 2022.04.17 |