To demonstrate the prototype pattern in action, I have created this class here called robots, which
implements skill levels.
So just remind you, you need to implement this interface in order to use to implement the code method
yourself.
So the robot class has three properties here.
One is very simple.
It's a primitive type.
The other one is a list of strings and the other one is another class called components.
The components class is very easy.
It has only two strings as as properties.
And I get the sense that there's for those properties here, you can imagine that this this object could
have been a lot more complicated than it is right now.
Even this one has a lot of I mean, has to two objects and this array can be on this list here, can
be very lengthy.
So it will occupy a fairly decent chunk from from the memory.
So it's it's actually very costly to have a lot of robots, objects created in your code.
Here is the constructor in the constructor.
I have initialize all the properties here and of course you have together since at the and the clone
method, which is very important for the prototype.
So here is a try catch block.
We just need to return the execution of the super class section of the clone and we need to cast the
result, the return of of this method to a robot, if not an object.
If this will will result in an exception on the object, will be will be returned and let's see this
in action.
So I have created the main method in this class here.
I have defined the features in the form of a retest.
And here, as you can see, I created some tasks that a robot can can perform.
Also, I added one component here just to make things easier to to understand.
And also I have initialize here the first robot.
So like I said in the previous video, we need to use the new keyword.
Only at the beginning.
The rest of the objects will be created using the clone method.
So as you can see here, another robot is created and we call the clone method of the first robot that
has been created within the cube using the constructor.
And the thing to demonstrate here is that all these two robots are the same.
However, these are two different objects.
So these will have two different addresses.
This will be stored in two different addresses in memory.
So even though they share the same properties, they will have that there will be two separate objects.
So let's run this and we should see the same.
Objects outputted, so as you can see, the same properties here are outputted, so we see I.D. number
one for both of them, which is not very correct in terms of of the logic for the application.
But you can anyway change the ID of your uncle using the center and component here is just it's just
the name of the class and the hash.
You can see it is the same and the actions that the robot can can create.
So as you can see, it works quite well.
So whenever you have a situation when you can have a class, when it is very costly to instantiate the
class twice, you and also you won't mind to have the same properties for the same for two different
instances, then you may want to consider the prototype either because it's a bit more efficient to
use it and also not very complicated, especially if you don't perform a deep clone, a copy of your
object if this is called a shallow copy.
So only the primitives and some some references of the objects are copied, not the objects themselves.
Right.
So not that component object.
Only just just a reference to this object.
So that's about it.
Regarding the prototype.
Join me in the next tutorial where we are going to discuss about object pull.
So see you in the next tutorial.