Kotlin Singleton Tutorial - Companion Object, Operator Invoke

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will learn about singleton pattern in object oriented programming singleton pattern means only a single object when we want to have a single instance of a class we can use this pattern and it is required in many use cases the most common use case is when you have an object that is expensive to create and you use this object very frequently then in this case you want to make only a single instance that is shared throughout your application now let's see how we create a singleton class and other programming language for example java now here i will use kotlin only but i will create a singleton class as we create in java so first we will have a class let's say we have a class named manager now here we want only a single instance then the very first thing that we do is we make the constructor of the class private so let's do it i will write here private and then constructor now the constructor of this class is private and we cannot instantiate this class let's see if i try to instantiate this class what will happen is i will get an error because the constructor of this class is private and i cannot access it here now the next thing that we do is we create a static instance of the class but in kotlin we do not have static keyword or we do not have static so instead of creating a static instance in kotlin because we cannot create a static instance in kotlin we will use companion object so it is a replacement of static and kotlin you can say so what i will do is i will write here companion and then object and inside this companion object i will create an instance of this manager so this is what we do when we want to create a singleton class we create a private instance of the class and the initial value is null now what we do is we create a function and that function is public static and that function will initialize this instance but before initializing we will check if the instance is already initialized in that case we will directly return the instance if the instance is not initialized we will initialize the class because we are inside the class only and we can access the private constructor so in case the instance is null we will initialize the instance and we will return the instance so this is how we create a singleton class in other languages like java so i will create a function let's say we have fun get instance and inside this function we will check if instance equals to equals to null then initialize it we have instance equals to manager if the instance is not null we will directly return the instance like this now in this case what we can do is we can simply write manager dot get instance and now if the instance is already initialized we will get the instance if the instance is null we will initialize the class and we will return the initialized instance so technically we should get only a single instance but this code is not thread safe so when two threads will try to access the same function at the same time we may have a duplicate object so to fix this thing what we can do is we can make it synchronized like this so now this is thread safe as well now if i will write manager.getinstance and i will print it let's say println and manager.getinstance now if i run the code you can see here we are creating only a single instance so this is how we achieve singleton in other languages of course it is kotlin but i created a singleton as we create in java now instead of using this get instance function what we can do is we can use invoke operator as well so this is also a thing that you should know so in kotlin we have operator fun invoke and this invoke is called whenever we write it like this so we have manager and then parentheses so this thing is same as writing manager dot invoke so we can do this thing as well we can use invoke operator both are the same thing we can just omit this invoke to make it more concise so this is the way we can create a singleton instance now the problem with this approach is we have a lot of boilerplate code because every time you want to create a singleton instance you need to write all of these things again and again so kotlin solved this thing and in kotlin you do not need to write this much of code to create a singleton class in kotlin what you can do is you can simply create an object manager like this and you are done you can have init block as well so for the example i will write here manager object initialized and now this is our singleton class and every time we will get only a single instance so let's run the code and you can see we have a single instance of manager and the init block is called only once because the first time you access an object it is initialized and then it uses the same instance so this is how kotlin simplifies the singleton pattern you just need to use the object keyword and it is same as class you can have init blocks but you cannot have constructors here as you can see if i try to write a constructor i will get an error that says constructors are not allowed for objects so this object is a singleton class and every time we will get only a single instance of this object and it is thread safe as well so instead of writing that much of boilerplate code you can just create an object if you want to achieve singleton pattern and kotlin so that is all for this video friends i hope you found this video helpful and learned something in case you have any question or confusion you can leave your comments below thanks for watching everyone this is bilal khan now signing off you
Info
Channel: Simplified Coding
Views: 5,448
Rating: 4.8994975 out of 5
Keywords: kotlin singleton, kotlin companion object, kotlin operator invoke, kotlin tutorial, kotlin oop, kotlin programming tutorial, android kotlin tutorial, learn kotlin, kotlin tutorial for beginners, kotlin object oriented programming tutorial, kotlin for beginners, android development tutorial
Id: Vuwa21Xy-dc
Channel Id: undefined
Length: 7min 46sec (466 seconds)
Published: Wed Jan 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.