Chapter 6. Prototype Design Pattern

Originality is nothing but judicious imitation. The most original writers borrowed one from another.

Voltaire

We forfeit three-quarters of ourselves in order to be like other people.

Arthur Schopenhauer

Act that your principle of action might safely be made a law for the whole world.

Immanuel Kant

What Is the Prototype Design Pattern?

The Prototype design pattern is interesting in its use of a cloning technique to replicate instantiated objects. New objects are created by copying prototypical instances. In this context, instances refer to instantiated concrete classes. The purpose is to reduce the cost of instantiating objects by using cloning. Rather than instantiating new objects from a class, a clone of an existing instance can be used instead. Figure 6-1 shows the Prototype class diagram.

Prototype class diagram

Figure 6-1. Prototype class diagram

Note that the Client class is an integral part of the Prototype design pattern. The client creates an instance of a concrete prototype through the Prototype interface that includes a clone method of some sort. Fortunately, PHP has a built-in clone() method that can be used within the design pattern. As you will see, the basics of the design are quite simple.

When to Use the Prototype Pattern

The Prototype pattern should be used in any application where your project requires that you create several instances of a prototypical object. ...

Get Learning PHP Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.