From the course: Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

How the pattern works

How the pattern works

From the course: Design Patterns: Creational

Start my 1-month free trial

How the pattern works

- [Instructor] We have a client that wants a new instance of an object. To create that instance, we'll use a prototype object and clone that prototype. All objects that want to act as prototypes must implement the prototype interface and implement a method, clone. This method makes a copy of the prototype object and returns it. We can have different kinds of prototype objects. As long as all the prototype objects implement the prototype interface, we know we can get a new instance of that type of prototype by calling its clone method. Notice too, that the pattern makes no prescription for how the clone method should be implemented. So that's entirely up to you as the software designer. You could use a built in method to copy objects if one exists. For instance in Java, objects that implement the cloneable interface use a clone method to copy themselves. You could even call new on the object's class and create a new…

Contents