1 00:00:00,750 --> 00:00:07,140 This is an example of a law that is implemented using the single term pattern, so we said previously 2 00:00:07,140 --> 00:00:14,220 that we cannot create two instances of the same object if we implement the Singleton pattern. 3 00:00:14,490 --> 00:00:17,610 So basically, we need to define this instance. 4 00:00:17,610 --> 00:00:23,740 The only instance that the object can can can take and instantiate, too. 5 00:00:24,570 --> 00:00:34,050 So here you can see that this we have this object here that this private is the type of the longer and 6 00:00:34,290 --> 00:00:41,090 the name is easy to interpret, to see what it is about. 7 00:00:41,430 --> 00:00:48,690 And we call reinitialize that with the value that is returned by our implicit constructor here. 8 00:00:49,420 --> 00:00:54,900 The constructor needs to be private because we don't want to allow the client to create objects using 9 00:00:55,170 --> 00:00:55,820 using it. 10 00:00:56,790 --> 00:01:04,620 So that's why in Singleton you need to have a private constructor and only public method here. 11 00:01:04,620 --> 00:01:12,420 That is part of the Singleton implementations that get instance, a method which will return this instance 12 00:01:12,420 --> 00:01:14,200 object to the client. 13 00:01:14,520 --> 00:01:20,720 This needs to be made public because it's the only way to get a grasp of this object. 14 00:01:20,730 --> 00:01:22,760 Otherwise you cannot access it at all. 15 00:01:23,220 --> 00:01:31,410 And those two Modesto's those less last methods here are actually the actions that the longer can perform. 16 00:01:31,440 --> 00:01:38,430 So basically it will output two lines in the console when the longer when the program is certain, when 17 00:01:38,430 --> 00:01:44,170 the program is stopped, you may notice that you may have noticed here this comment. 18 00:01:44,190 --> 00:01:50,760 So basically, this is the Eger initialization method for implementing a singleton. 19 00:01:51,240 --> 00:01:56,370 And this means that the object is preloaded. 20 00:01:57,270 --> 00:02:04,270 So it's not it's not loaded whenever you want it to to be used. 21 00:02:04,290 --> 00:02:10,630 So whenever you call it, actually, it will be preloaded in your program. 22 00:02:10,650 --> 00:02:18,660 So if you have a memory problem in your program, if if your application is consuming a lot of memory, 23 00:02:18,930 --> 00:02:22,230 you may need to drop this implementation. 24 00:02:22,440 --> 00:02:30,300 So I will come in this up and use lazy desalinisation method. 25 00:02:30,300 --> 00:02:35,180 So I will I will show you how to do this. 26 00:02:35,190 --> 00:02:38,070 So basically, we need to copy this instance here. 27 00:02:39,450 --> 00:02:41,310 We do not initialize it. 28 00:02:41,310 --> 00:02:42,740 We will leave it as it is. 29 00:02:42,750 --> 00:02:50,110 And here in the get instance method, we will make it a bit more a bit more complicated, let's say. 30 00:02:50,140 --> 00:02:59,400 So if this is a recipe, we need to ensure that the instance is not know if it is no, then we will 31 00:03:00,060 --> 00:03:00,720 create. 32 00:03:02,770 --> 00:03:07,890 But Singleton, no single, no longer here. 33 00:03:14,840 --> 00:03:25,270 And also to add this single to Lowgar, object to the instance itself, and then we will return Dickinsons, 34 00:03:25,640 --> 00:03:31,200 this is the and this is a way to make it to make our program more memory efficient. 35 00:03:31,220 --> 00:03:39,980 So if the program will not need a single object at all, it will create the instance for it. 36 00:03:40,370 --> 00:03:45,140 Whereas for the Icarian sanitisation, you will have it preloaded in your program. 37 00:03:45,150 --> 00:03:49,900 And it's not that memory efficient to use this type of organization. 38 00:03:50,480 --> 00:03:57,720 Another aspect that you may take care of that you may need to take care of is having your Singleton 39 00:03:57,740 --> 00:03:58,990 object thread safe. 40 00:03:59,510 --> 00:04:08,090 So if you are having a multithreaded, you are working on multithreaded application and two threads 41 00:04:08,090 --> 00:04:14,030 will try to access your Singleton object at the same time, it may result in an exception. 42 00:04:14,360 --> 00:04:21,600 So basically to make it safe, you just need to put the key synchronous here and there you have it. 43 00:04:22,340 --> 00:04:27,800 This is a singleton that is you know that it's a synchronize, which means it's safe. 44 00:04:28,370 --> 00:04:36,990 So this is these are some aspects that you really need to think of when implementing your singleton 45 00:04:37,040 --> 00:04:37,480 object. 46 00:04:37,850 --> 00:04:44,240 If you do not have any issues with the memory in your program, you can use securitization to make it 47 00:04:44,240 --> 00:04:45,610 look a lot simpler. 48 00:04:45,950 --> 00:04:52,880 And if you're not using multi trending, if you if your program is not operating in a multi threaded 49 00:04:52,880 --> 00:04:57,530 environment, then you may need to drop the synchronized key here. 50 00:04:57,530 --> 00:05:00,480 But otherwise this is the way to go. 51 00:05:00,890 --> 00:05:03,080 So let's see this thing actually here. 52 00:05:03,950 --> 00:05:09,890 I created here a Singleton logger by calling the get instanced method. 53 00:05:09,900 --> 00:05:17,840 So like I said, the getting system is the only way to get the object, the instance that you need of 54 00:05:17,840 --> 00:05:19,500 your other instance of your object. 55 00:05:20,030 --> 00:05:29,750 So this will get our Singleton logger here and then we will output two messages for start and stop. 56 00:05:29,780 --> 00:05:31,160 So let's see this in action. 57 00:05:36,880 --> 00:05:43,830 Right, so you can see it's behaving as expected, so this is the single implementation. 58 00:05:44,140 --> 00:05:45,790 Join me in the next video. 59 00:05:45,930 --> 00:05:48,460 We are going to discuss about the builder father.