Prototype : Design Pattern

Dec 22, 2022 By Puneet verma

The prototype design pattern is a creational design pattern that allows you to create new objects by copying existing objects. This can be a faster way to create objects than creating them from scratch, especially if the creation process is complex or time-consuming.

Let's understand it with an example:

Suppose you developed software that scans text from images. You applied a lot of algorithms and image-processing techniques to get the text out of the image.

The client wants a PDF, and you do all process and convert it to PDF. The client has changed his mind and wants it in word format. Again you will process and convert the desired format.

A prototype design can be a saviour for us. It would save us from re-processing. Once we process the image, we can clone it and use it as we wish!!!

 

The prototype design pattern can be useful in a number of situations. Here are some reasons you might use the prototype design pattern:

  1. When creating an object is expensive or time-consuming: If creating an object involves a lot of work, such as allocating resources or performing complex calculations, you can use the prototype design pattern to create new objects by copying existing ones. This can be faster than creating the objects from scratch.

  2. When you want to avoid creating multiple objects with the same properties: If you have multiple objects with the same properties, you can use the prototype design pattern to create new objects by copying an existing object instead of defining the properties again. This can save time and reduce the risk of errors.

  3. When you want to allow clients to create new objects by modifying existing ones: The prototype design pattern allows clients to create new objects by modifying existing ones without knowing the details of the objects' implementation. This can be useful if you want to provide a flexible way for clients to create objects without exposing the implementation details.

  4. When you want to reduce the number of subclasses: The prototype design pattern can help reduce the number of subclasses you need to create because you can create new objects by copying existing ones instead of defining a new subclass for each variation.

What is the difference between creating an object using a new  keyword vs cloning

  • If you create a new object, you have to go through the initial step, which is saved when coping. 
  • Copying existing ones instead of defining all of the data and methods for the object again. This can save time and reduce the risk of errors.

 

Here's the basic structure of the prototype design pattern:

<?php

class Book{
    protected $title;
    protected $topic;

    public function getTitle(){
        return $this->title;
    }
    public function setTitle($title): void{
        $this->title = $title;
    }
    public function getTopic(){
        return $this->topic;
    }
    public function setTopic($topic): void{
        $this->topic = $topic;
    }
}


$phpBook1  = new Book();
$phpBook1->setTitle("PHP basic");
$phpBook1->setTopic("Topic 1");

$phpBook2 = clone $phpBook1;
$phpBook2->setTopic("Topic 2");

$phpBook3 = clone $phpBook2;
$phpBook3->setTitle("PHP advance");


writeln('Book '.$phpBook1->getTopic()." topic ".$phpBook1->getTitle()); // Book Topic 1 topic PHP basic
writeln('Book '.$phpBook2->getTopic()." topic ".$phpBook2->getTitle()); // Book Topic 2 topic PHP basic

writeln('Book '.$phpBook3->getTopic()." topic ".$phpBook3->getTitle()); // Book Topic 2 topic PHP advance

We first initialized $phpBook1. Then we copied it to $phpBook2 to reuse it in a different way.

This enables to have a variation of $phpBook1 with having any subclasses.