Flutter | Dart | Design Pattern : Singleton Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let us look into the creational design pattern and today we will be looking into the uh Singleton pattern and the creational design pattern is a way that provides us a way to create an object by actually hiding its uh creational logic so how the object are created it gets hidden inside and this provides more flexibility on deciding which object that we want to create right we don't have to directly create an any object like a final final user equals to new user or final user equals to user so we don't have to directly instantiate the object but it provides us a way to create an object and in a more flexible way so let's go and create a new file over here and I'll call it as a singleton.tr.file and here let's create a class and I'll call it as a person and let's say we have one variable of a name and let's create a Constructor first this one and this is the thing name so once we have this let's also overwrite the two string function and from here we can just return the string object let's see the person and I can just say um dollar the name that we can have okay and then we can also uh overwrite the another function of let's say this one is server type written type is available operator which is of equals to equals to D let's create an object so I guess let's remove this one and then we have uh let's say object type and this is of the object that we hit and here let's decide the if that particular object that we are receiving if the object is the type of the person right then we are going to check some condition over here if the uh this dot the name if this that the name is equals equals to the object that the name that we are passing in then we will say that this is a same object so we'll say written true right otherwise we will just return a false from here so a written up false so this means that we have the operator that checks actually two object of this person is a same or not so let's go and have a main function over here and let's say uh final P1 let's say which is f equals to the percent over here and we'll pass in the name as something like the origin let's call it as a region and then uh we can just say uh and create another one so final P2 which is of equals to the person and here we'll pass in the name as the let's say the one and then let's have a final and P let's say P3 which is of equals to the person and let's have the same name um right so that should be fine it should be Alpha name okay so now what we'll do is we'll try to check if uh all of these are the same so let's go and print out over here and I just want to let's see a print over here and let's try to print the P1 right and let's try to print the B2 and then we also print the uh let's see of P3 and let me run this method and we can get that the person Richard person they want Personnel Richie now let us try to check all of this uh again so I'll just copy this again let's go down below over here and I'll just put it inside the string and let's do the string interpolation over here first of all and that should we have a dollar and let's do it of a dollar sign in dollar sign and here we want to check something so let's say that the P1 sorry I need a string interpolation so so that should be of a P1 dot the hash code and I will do the same thing for rest of this so let's go over here and let's go over here and I'll just run this one and you can see that the passcode for each of them oh it's the same why because we have the same object over here this is V2 and it should be for P3 so now again if you run we should get a different high score here we get a different s code here we get a different Hesco and here we get a different hash code even though the this B3 and the P1 has the same object which is of the original and written but their hashcode is different so uh we can also check like um print over here and which is of the let's do this string interpolation of the P1 sorry and taller let's do it inside this one over here so uh P1 sorry it should be of a dollar and then the P1 which is of uh equals equals to the p 2 right so we have a P2 over here foreign and you get a false over here the false means that even though that these contain the same object we are getting the false over there also it should be of a p390 P2 and then we'll just return it over here it should it's written in a true over here even though the hash code is different so it's just checking the uh here the object of the variable name is same or not but what we want to do over here is uh in this case we want to have a SIM has code which means that the memory location of this particular object should be same and it should be created only once so whenever we create a multiple object of this particular person uh we just want to have a one object for them we don't have to we don't want to have multiple objects which means here it is creating a multiple object over here so what we can do is um let's go up and let's copy this logic over here and what I will do is instead of person let's say this is server um let's say this is our car car object and here I'll just say a car and let's override this one as a car that name and uh let's override these two this should be of a car and uh then this should be a name okay that should be fine and what we'll do over here is instead of creating an object directly what we want to do is we want to create a Singleton object over here so let's create a Singleton object for this one so let's go over here first of all and I'll just see a car and I'll create a secondary Constructor which is of the let's say uh internal object and this will be of our secondary Constructor and here we'll create a final and I'll just I think I'll create a static final and let's create of the car object which is of the let's say uh instance is off equals to the car that we have and the internal so this will return as the internal object from here so whenever we call the instance it should return as the internal object it will create a car object right and here what will this will use a Factory sorry facet you are a factory Constructor and with this I can just remove this part and just see a name over here we are taking a name and let me put it inside over here so once we have this we can just say that we have an instance which is of a card instance that we are creating and instead of the name we can just set the name that we are getting in and then we can have the return type from here which we will return the instance because it's a final so we can have the final over here it should be of lead this is a tribe which of the string uh s3ri Eng string so that should be fine so now you can see that we have the um variable name of the string and then we have a static which is of the instance of this car and when it's called it's written the internal Constructor which is of this one this is a private Constructor which is a secondary Constructor in the DOT and now uh let's go down over here and I guess I'll just copy this one too I I think I'll just comment on this one first of all and let's create a final C1 which is of equals to the uh car and then I'll pass the uh name that we have let's say um to your time right and let's do that one and I'll create another one final C2 which is of equals to D car and then we will as we need a name and the name I'll just give it as a Honda let's say the Honda and then let's say uh what we will do now is we'll just go and print the object of a c once yeah C1 and also we will print out the object for the uh C2 right so let's see if anything is wrong over here so it's C1 and dc2 C1 and C2 so let's run this one so we get the Honda and the Honda again so why is is it that it's a C1 and C2 and even we have created two different objects you can see it has been replaced by the another knobs which have a Honda and now if I add another one final C3 and this is of equals to the car and let's say that this is of the uh body and let's have a name for this one and if I now go and print that one and let's see a print and then I have a C3 object over here and let's try to run this one then you can see all of these particular C1 C2 and C3 is having the same value of the audio why is that because we just have a single instance what it does is effective Factory Constructor over here is going to create a single instance if that instance is already created so it's just going to return that instant and then it gets replace so you can see that by calling that particular hash code so if I want to see the hash code and see check if this has the same memory location in inside our memory so I can just say see dollar C1 that we have sorry dollar C1 and then I can just use a string interpolation of the C1 dot the hash code and I'll do the same thing for all of our C2 and DC3 so this is of a C2 and this is safety C3 and here we have a C2 and here we'll have a C3 so let's run this one and you can get get that this particular object all of them have the same memory location which means that even we create a multiple instance of that particular object we are going to get the same instance okay so all of these objects have the same memory location so that's why we are getting just a single instance of this particular object so this is really helpful if you just want to have a single instance of your application uh sorry the of this particular object throughout your application lifestyle so which means that like some of your services like your database services and your network Services you can have a single instance of that and because the database service you cannot recreate every time it will take a lot of the resources as well as your transaction may get corrupted like if you are updating and again you destroy it and again you create recreate it it is it possible that the you your transition may get corrupted or even the database get corrupted so you can have a single instance for this kind of the object creation and to be there is also a drawback like if you create this particular object it's not going to get destroyed throughout your application which means your garbage collection is not able to collect this object so it's uh west of wastage of your memory too so it will take up your resource and your memory for your application so when you are using a Singleton you just need to be clear that it has some drawback as well as the the plus point and the negative point and you just want to decide whether you need to have this particular object as a Singleton and and you decide it and then only you can create it and don't just simply go and create the Singleton inside everywhere and that will affect the performance of your application as well as it's not a good practice to have a Singleton everywhere it's only in some of the um in some of the cases like your your database Services your network services so all of those classes should be a Singleton so just decide yourself which class you need as a Singleton object and which of them you don't need uh so that's all for this lesson
Info
Channel: Mobile Academy
Views: 670
Rating: undefined out of 5
Keywords: flutter, dart, android, ios, design pattern
Id: 1ZdOf9bTq0c
Channel Id: undefined
Length: 15min 34sec (934 seconds)
Published: Mon Sep 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.