O'Reilly logo

Learning PHP Design Patterns by William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required