How to Create a Singleton in Kotlin #androidstudio #android #kotlin

preview_player
Показать описание
How to Create a Singleton in Kotlin #androidstudio #android #kotlin

In this lesson, you'll learn how to create a singleton in Kotlin. You'll also learn what they are and why you'd want to use one.
How to create a Singleton class in Kotlin?
we use the Singleton Pattern. Singleton Pattern is a software design pattern that restricts the instantiation of the class to only “one” instance. So, to implement the Singleton pattern in our project or software, we make a singleton class. In this blog, we will learn how to make a singleton class in Kotlin? So, let’s get started.

Singleton Class
A singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere.

It is used where we need only one instance of the class like NetworkService, DatabaseService, etc.

Generally, it is done because it takes the resource of the system to create these objects again and again. So it is better to create only once and use again and again the same object.

Properties of Singleton Class
Following are the properties of a typical singleton class:

Only one instance: The singleton class has only one instance and this is done by providing an instance of the class, within the class. Also, outer classes and subclasses should be prevented to create the instance.
Globally accessible: The instance of the singleton class should be globally accessible so that each class can use it.

Rules for making a class Singleton
The following rules are followed to make a Singleton class:

A private constructor
A static reference of its class
One static method
Globally accessible object reference
Consistency across multiple threads

class Singleton {

companion object {
val instant = Singleton()
}

}

In Kotlin, we need to use the object keyword to use Singleton class. The object class can have functions, properties, and the init method. The constructor method is not allowed in an object so we can use the init method if some initialization is required and the object can be defined inside a class. The object gets instantiated when it is used for the first time.

Singleton Class in Kotlin is also called as the Singleton Object in Kotlin. Singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere. Many times we create the two different objects of the same class, but we have to remember that creating two different objects also requires the allocation of two different memories for the objects. So it is better to make a single object and use it again and again.

So when we use an object instead of a class, Kotlin actually uses the Singelton and allocates the single memory. In Java, a singleton class is made making a class named Singleton. But in Kotlin, we need to use the object keyword. The object class can have functions, properties, and the init method. The constructor method is not allowed in an object so we can use the init method if some initialization is required and the object can be defined inside a class. We call the method and member variables present in the singleton class using the class name, as we did in the companion objects

Importance of Singleton Objects In Android
Below are some points which explain the importance of singleton objects in android along with some examples, where it must be used, and reasons for why android developers should learn about singleton objects.

As we know that when we want a single instance of a particular object for the entire application, then we use Singleton. Common use-cases when you use the singleton is when you use Retrofit for every single request that you make throughout the app, in that case, you only need the single instance of the retrofit, as that instance of the retrofit contains some properties attached to it, like Gson Converter(which is used for conversion of JSON response to Java Objects) and Moshy Converter, so you want to reuse that instance and creating a new instance again and again would be a waste of space and time, so in this case, we have to use singleton objects.
Consider the case when you are working with the repository in MVVM architecture, so in that case, you should only create only 1 instance of the repository, as repositories are not going to change, and creating different instances would result in space increment and time wastage.
Suppose you have an app, and users can Login to it after undergoing user authentication, so if at the same time two user with same name and password tries to Login to the account, the app should not permit as due to concern of security issues. So singleton objects help us here to create only one instance of the object, i.e user here so that multiple logins can’t be possible. Hope these examples are sufficient to satisfy the reader to explore more about singleton objects in Kotlin so that they can use singleton object in their android projects.
Singleton Singleton Singleton
Рекомендации по теме
visit shbcf.ru