Prototype design pattern
Pattern designs were classified into three general categories in 1994 by a group called the Gang of Four. Design patterns that are developed to manage the creation of objects are called creational design patterns. Prototype design pattern is one of the constructor design patterns used to prevent the construction of a new object.
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 creating a 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 each having a different color. Using the prototype design template, you no longer need to create a new object of class 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 Earl’s karmic-driven world. For the native of this aircraft, the only way forward will be to know exactly the parameters and structural features of its external prototype. However, 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. By doing this, you can import a foreign aircraft into that device and receive your simulation aircraft. 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 main object and apply it to the new object. However, 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 private 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, new objects become dependent on these classes, and this creates a lot of dependencies between classes.
Prototype design pattern solution
In the prototype design pattern, a common interface is provided for all objects to be copied. This interface allows you to copy an object without having to rewrite the code in the object class. Typically, 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 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 the values of its parameters to the new object. Another advantage of this design template 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. By doing this, 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 that are configured in different ways. Then whenever you need an object with one of the configured configurations, just create a prototype based on it and you will no longer need to build a new object from scratch.
An example in the real world
In the real world, prototypes are used to perform various tests before mass production begins. 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, almost the best example of this design pattern in the real world is the cell division process.
Imagine defining a class called a cell in the software. In order 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 make up the different organs of a living thing, each with its own 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. There will no longer be a need to make the cells and set their parameters based on the original cell one by one. Given the fact that in the process of 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
Applying 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 the operation of copying objects, the constructor function will no longer be executed. The constructor design pattern will be used in the following cases:
- Used to reduce the number of child classes (Subclass). Also, with this method, the constructed objects can be configured according to different needs.
- Used to prevent dependence between the copied object and the class.
- When we want to create an object from a class with many Private parameters.
Conclusion
A prototype design pattern is used to prevent the construction of a new object. 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.