Lecture thumbnail 0:03 / 1:59 In this section of the course.

We’re going to take a look at the prototype design pattern.

And the prototype design pattern is all about object copying.

So we’re going to delve deep into the replication of objects.

Now, what is the motivation for using the prototype?

Well, it just so happens that complicated objects like cars or iPhones, for example, they’re not

really designed from scratch.

If you think that any time a factory wants to make a new model of a car, they just do it from scratch.

Well, you’d be mistaken.

They take an existing design of a car and they change it.

They improve it somehow.

And essentially they reiterate existing designs, making them better, making them more palatable to

the consumer.

And the prototype design pattern is exactly the same approach in software engineering.

So essentially the prototype is an existing object which can be partially or fully constructed.

So you’ve already designed something you’ve already defined, for example, a very complicated type.

You’ve initialized it.

Maybe you used a builder to initialize it and then you simply want to make variations on it.

So in order to make variations, what you need to do is you need to make a copy of the prototype and

then customize it.

And then making a copy is also sometimes called cloning.

But what we really need is we need something called deep copy.

Deep copy is a situation where you are copying not just the object, but you’re copying all of the objects

references by making new objects which replicate the state of those references.

And you’re doing it recursively, so you need to make a complete copy of the object.

So the changing this object doesn’t affect the object it just copied and we have to make the cloning

convenient.

So we have to provide an API, like, for example, a factory to make the cloning of existing objects

a convenient affair.

So the prototype is quite simply a partially or fully initialized object that you make a copy of and

then you subsequently use for your own benefit.

Stop Play Play Play Play Play Start Play information alert