blog posts

What are Prototype Design Patterns ?

Pattern designs were classified into three general categories in 1994 by the Gang of Four group. Design patterns developed to manage the creation of objects are called creational design patterns. A prototype design pattern is one of the constructor design patterns used to prevent the construction of a new object.

Prototype pattern design pattern

This design template provides a way to create a new object by copying it from other existing objects. Using this technique, new objects can be created in the software without dependency. Between the new object and the class from which it is made; also, in cases where there is a need to make changes in new objects. So that we can create objects of the same type and with different parameters.

For example, imagine that you want to make multiple copies of a machine object, with the difference that each has a different color. Using the prototype design template, you no longer need to create a new class object each time and then specify its parameters because you can create an object from the machine class and then make as many clones as you need.

Why should we use the Prototype design template?

Imagine for a second you were transposed into the karmic-driven world of Earl. For the native of this aircraft, the only way forward will be to know exactly the parameters and structural features of its external prototype. But since aircraft technologies are proprietary and not readily available to other companies, this will not be possible for you. But imagine having a giant 3D copier to simulate objects. You can import a foreign aircraft into that device and receive your simulation aircraft by doing this. This simulator does the same thing as the prototype design pattern.

 

So if you want to make an exact copy of an object in the software, you must first create a new object of the desired class. Then you have to get all the parameters and properties of the original object and apply them to the new object. But this method will not always work properly because some parameters and properties of an object may be defined privately. In this case, it will no longer be possible to access them from outside the classroom.

Therefore, one of the problems of copying objects directly in programming is the lack of access to some secret parameters. So it will not always be possible to copy an object outside of its class. On the other hand, because you create duplicate objects based on the classes of the source object, the new objects become dependent on those classes, and this will cause a lot of dependencies between the classes.

Prototype design pattern solution

The prototype design pattern provides a common interface for all objects to be copied. This interface allows you to copy an object without rewriting the code in the object class. Usually, such an interface contains only one function called a clone. So by following the object classes of this interface, we will no longer need to write additional functions or use them to recreate an object. All you have to do is implement the Clone function in the classes.

The implementation of the Clone function is very similar in all classes. This function has the task of creating an object of the current class and then transferring all its parameter values ​​to the new object. Another advantage that this design template gives you is that you can even copy Private parameters because most programming languages ​​allow objects to access private fields from other objects that belong to a class.

Objects whose classes support copying are called prototypes. Also, when objects have dozens of parameters and hundreds of settings, you can use duplicate objects as a substitute for child classes. You will no longer need to build child classes from the main class in the software because you can modify objects based on the main class to suit your needs.

 

So if we want to summarize how this template works, we must first create a set of objects configured in different ways. Then whenever you need an object with one of the configured configurations, create a prototype based on it, and you will no longer need to build a new object from scratch.

An example of a real-world prototype design pattern

Prototypes are used to perform various tests before mass production begins in the real world. But in this design pattern, prototypes are not used for various tests or quality control. The samples made in this technique are used directly in the required parts in the software. Therefore, the cell division process is almost the best example of this design pattern in the real world.

 

Imagine defining a class called a cell in software. For this cell to become an evolved organism, it needs to multiply and prototype. So we will need thousands or millions of objects in this class to form the various organs of a living thing, each with its task. Now imagine how much of your time would be wasted if you were to build all of these objects directly and one by one and quantify their parameters.

Accordingly, the best way to do this would be to use a prototype design pattern. Because in this model, the cells will be copied based on the original cell, and there will be no need to make cells and set their parameters based on the original cell one by one. Given that in cell division, new cells are created from the original cell, it can be said that the original cell acts as a prototype.

Prototype design pattern applications

 

Using a prototype design pattern in software design will reduce the amount of coding. The main advantage of this pattern design is the increased speed of copying an object from an object. Because it is much faster than making objects, in copying objects, the constructor function will no longer be executed. The constructor design pattern will be used in the following cases:

  • They are used to reduce the number of child classes (Subclass). Also, with this method, the constructed objects can be configured according to different needs.
  • It is used to prevent dependency between the copied object and the class.
  • When we want to create an object from a class with many Private parameters.

Implement a prototype design pattern

In this section, we will review the implementation steps of the prototype design template; then, we will implement this template in the form of an example using PHP programming language. The implementation steps of this template are:

  1. You must first define an interface. Then create a function called Clone. The desired classes must then follow this interface and implement the Clone function. This step regulates the software and can be ignored if detected.
  2. Then in the prototype class, a new Constructor method called Clone must be defined. This constructor is responsible for receiving the values ​​of the parameters defined in the class from the original object and transferring them to the newly created instance.

An example of a prototype design pattern in the real world

This example shows you how to copy a page with many parameters using the prototype template. The page class has many Private parameters passed to the copied object thanks to the prototype pattern. Because if we want to save a draft from this page, we must receive all its parameters, which will not be possible without using this method due to the privacy of the class parameters.

<? php 
/ **
 * Prototype.
 * / 
class  Page
 {
     private  $ title ;
    private  $ body ;
    / **
     * @var Author
     * / 
    private  $ author ;
    private  $ comments = [];
    / **
     * @var \ DateTime
     * / 
    private  $ date ;
    // +100 private fields. 
    public  function  __construct ( string  $ title , string  $ body , Author $ author )
     {
         $ this -> title = $ title ;
        $ this -> body = $ body ;
        $ this -> author = $ author ;
        $ this -> author-> addToPage ( $ this );
        $ this -> date = new \ DateTime;
    }
    public  function  addComment ( string  $ comment ): void
     {
         $ this -> comments [] = $ comment ;
    }
    / **
     * You can control what data you want to carry over to the cloned object.
     *
     * For instance, when a page is cloned:
     * - It gets a new "Copy of ..." title.
     * - The author of the page remains the same. Therefore we leave the
     * reference to the existing object while adding the cloned page to the list
     * of the author's pages.
     * - We do not carry over the comments from the old page.
     * - We also attach a new date object to the page.
     * / 
    public  function  __clone ()
     this $ this -> title = "
         Copy of" . $ this -> title;
        $ this -> author-> addToPage ( $ this );
        $ this -> comments = [];
        $ this -> date = new \ DateTime;
    }
}
class  Author
 {
     private  $ name ;
    / **
     * @var Page []
     * / 
    private  $ pages = [];
    public  function  __construct ( string  $ name )
     {
         $ this -> name = $ name ;
    }
    public  function  addToPage ( Page $ page ): void
     this $ this -> pages [] = $
         page ;
    }
}
/ **
 * The client code.
 * / 
function  clientCode ()
 author $ author =
     new Author ( " Mehdi Allameh" );
    $ page = new Page ( "Tip of the day" , "Keep calm and carry on." , $ author );
    // ... 
    $ page -> addComment ( "Nice tip, thanks!" );
    // ... 
    $ draft = clone  $ page ;
    echo  "Dump of the clone. Note that the author is now referencing two objects. \ n \ n" ;
    print_r ( $ draft );
}
clientCode ();

Conclusion

A prototype design pattern prevents a new object from being built. Using this technique in object-oriented programming will have many advantages. As you can see, unlike some other design patterns, implementing this pattern design is very simple. What applications do you think this design pattern can be used in software? Have you ever used this template?