Open In App

How to Use Singleton Pattern for Room Database in Android?

Prerequisite:

Room is one of the Jetpack Architecture Components in Android. This provides an abstract layer over the SQLite Database to save and perform the operations on persistent data locally. This is recommended by Google over SQLite Database although the SQLite APIs are more powerful they are fairly low-level, which requires a lot of time and effort to use. But Room makes everything easy and clear to create a Database and perform the operations on it. The singleton pattern is one of the simplest design patterns. Sometimes we need to have only one instance of our class for example a single DB connection shared by multiple objects as creating a separate DB connection for every object may be costly. Similarly, there can be a single configuration manager or error manager in an application that handles all problems instead of creating multiple managers. In short, we use the Singleton pattern when we need to instantiate a single instance of a class. We define a class in such a way that only one instance can be created.



Where can we use the Singleton pattern in Android development?

A few of the examples where we can use Singleton Pattern in Android are:

There are other situations as well where we prefer to use the Singleton pattern. In this article, we will focus on How to use the Singleton pattern in android. As we have got the idea behind using the Singleton pattern in the Database, Now we shall further move on for the implementation.



Implementation

Here, We will Implement the Room database(assuming a database to store contacts) using the Singleton pattern.

Refer to the below code for reference.




import androidx.room.Entity
import androidx.room.PrimaryKey
  
@Entity(tableName = "contact")
data class Contact(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val name: String,
    val phoneNum: String
)

(autoGenerate = true) is used to auto-increment to id whenever a new data is added.

Refer to the below code for reference.




import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
  
@Dao
interface ContactDao {
    @Insert
    fun insert(contact: Contact)
  
    @Query("Select * From contact")
    fun getContact():List<Contact>
}

Refer to the below code for reference.




import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
  
@Database(entities = [Contact::class], version = 1)
abstract class ContactDatabase : RoomDatabase() {
    abstract fun contactDao(): ContactDao
  
    companion object {
        private var INSTANCE: ContactDatabase? = null
        fun getDatabase(context: Context): ContactDatabase {
            if (INSTANCE == null) {
                synchronized(this) {
                    INSTANCE =
                        Room.databaseBuilder(context,ContactDatabase::class.java, "contact_database")
                            .build()
                }
            }
            return INSTANCE!!
        }
    }
}

So, this companion object is common whenever you want to use the Singleton pattern in the Room database.


Article Tags :