1 00:00:01,070 --> 00:00:08,360 To demonstrate the prototype pattern in action, I have created this class here called robots, which 2 00:00:08,360 --> 00:00:09,350 implements skill levels. 3 00:00:09,360 --> 00:00:17,790 So just remind you, you need to implement this interface in order to use to implement the code method 4 00:00:17,810 --> 00:00:18,430 yourself. 5 00:00:18,920 --> 00:00:24,140 So the robot class has three properties here. 6 00:00:24,500 --> 00:00:25,370 One is very simple. 7 00:00:25,370 --> 00:00:26,660 It's a primitive type. 8 00:00:28,070 --> 00:00:34,160 The other one is a list of strings and the other one is another class called components. 9 00:00:34,160 --> 00:00:35,930 The components class is very easy. 10 00:00:35,930 --> 00:00:38,780 It has only two strings as as properties. 11 00:00:38,780 --> 00:00:45,530 And I get the sense that there's for those properties here, you can imagine that this this object could 12 00:00:45,530 --> 00:00:49,430 have been a lot more complicated than it is right now. 13 00:00:50,130 --> 00:00:57,080 Even this one has a lot of I mean, has to two objects and this array can be on this list here, can 14 00:00:57,080 --> 00:00:58,670 be very lengthy. 15 00:00:58,680 --> 00:01:05,240 So it will occupy a fairly decent chunk from from the memory. 16 00:01:05,250 --> 00:01:14,340 So it's it's actually very costly to have a lot of robots, objects created in your code. 17 00:01:14,960 --> 00:01:17,540 Here is the constructor in the constructor. 18 00:01:17,930 --> 00:01:25,220 I have initialize all the properties here and of course you have together since at the and the clone 19 00:01:25,220 --> 00:01:27,850 method, which is very important for the prototype. 20 00:01:28,730 --> 00:01:31,160 So here is a try catch block. 21 00:01:31,460 --> 00:01:40,790 We just need to return the execution of the super class section of the clone and we need to cast the 22 00:01:40,790 --> 00:01:48,290 result, the return of of this method to a robot, if not an object. 23 00:01:48,290 --> 00:01:53,660 If this will will result in an exception on the object, will be will be returned and let's see this 24 00:01:53,660 --> 00:01:54,160 in action. 25 00:01:54,350 --> 00:01:57,890 So I have created the main method in this class here. 26 00:01:58,310 --> 00:02:03,170 I have defined the features in the form of a retest. 27 00:02:03,260 --> 00:02:09,410 And here, as you can see, I created some tasks that a robot can can perform. 28 00:02:10,970 --> 00:02:20,350 Also, I added one component here just to make things easier to to understand. 29 00:02:20,360 --> 00:02:25,040 And also I have initialize here the first robot. 30 00:02:25,520 --> 00:02:30,260 So like I said in the previous video, we need to use the new keyword. 31 00:02:30,260 --> 00:02:31,300 Only at the beginning. 32 00:02:32,360 --> 00:02:35,860 The rest of the objects will be created using the clone method. 33 00:02:36,260 --> 00:02:43,430 So as you can see here, another robot is created and we call the clone method of the first robot that 34 00:02:43,430 --> 00:02:47,240 has been created within the cube using the constructor. 35 00:02:47,570 --> 00:02:54,280 And the thing to demonstrate here is that all these two robots are the same. 36 00:02:54,800 --> 00:02:56,990 However, these are two different objects. 37 00:02:56,990 --> 00:02:59,030 So these will have two different addresses. 38 00:02:59,040 --> 00:03:04,250 This will be stored in two different addresses in memory. 39 00:03:04,640 --> 00:03:12,500 So even though they share the same properties, they will have that there will be two separate objects. 40 00:03:12,800 --> 00:03:15,830 So let's run this and we should see the same. 41 00:03:16,900 --> 00:03:25,300 Objects outputted, so as you can see, the same properties here are outputted, so we see I.D. number 42 00:03:25,300 --> 00:03:32,260 one for both of them, which is not very correct in terms of of the logic for the application. 43 00:03:32,260 --> 00:03:41,800 But you can anyway change the ID of your uncle using the center and component here is just it's just 44 00:03:42,250 --> 00:03:43,690 the name of the class and the hash. 45 00:03:44,540 --> 00:03:51,250 You can see it is the same and the actions that the robot can can create. 46 00:03:51,260 --> 00:03:53,470 So as you can see, it works quite well. 47 00:03:53,740 --> 00:04:04,990 So whenever you have a situation when you can have a class, when it is very costly to instantiate the 48 00:04:04,990 --> 00:04:12,880 class twice, you and also you won't mind to have the same properties for the same for two different 49 00:04:12,880 --> 00:04:20,500 instances, then you may want to consider the prototype either because it's a bit more efficient to 50 00:04:20,500 --> 00:04:28,630 use it and also not very complicated, especially if you don't perform a deep clone, a copy of your 51 00:04:28,630 --> 00:04:31,060 object if this is called a shallow copy. 52 00:04:31,070 --> 00:04:37,980 So only the primitives and some some references of the objects are copied, not the objects themselves. 53 00:04:38,020 --> 00:04:38,370 Right. 54 00:04:38,380 --> 00:04:40,360 So not that component object. 55 00:04:40,360 --> 00:04:42,320 Only just just a reference to this object. 56 00:04:43,000 --> 00:04:44,350 So that's about it. 57 00:04:45,310 --> 00:04:46,680 Regarding the prototype. 58 00:04:46,800 --> 00:04:51,240 Join me in the next tutorial where we are going to discuss about object pull. 59 00:04:52,270 --> 00:04:53,740 So see you in the next tutorial.