blog posts

pattern

Prototype design pattern

In 1994, a group called the Gang of Four classified pattern designs into three general categories. 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.

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, we can create objects of the same type and with different parameters.

For example, imagine you want to make multiple copies of a machine object, each with 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 its external prototype’s parameters and structural features. 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, 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 them 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, accessing them from outside the classroom will no longer be possible.

Therefore, one of the problems of copying objects directly in programming is the lack of access to some private parameters. So, copying an object outside its class will not always be possible. On the other hand, because you create duplicate objects based on the classes of the source object, new objects become dependent on these classes, creating many dependencies between classes.

Prototype design pattern solution

A common interface is provided in the prototype design pattern 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 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 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, to summarize how this template works, we must first create a set of objects configured differently. 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 perform various tests before mass production begins. However, in this design pattern, prototypes are not used for various tests or quality control. The samples made using this technique are used directly in the required parts of the software. Therefore, the cell division process is probably the best example of this design pattern in the real world.

Imagine defining a class called a cell in the software. For this cell to become an evolved organism, it must multiply and be prototyped. 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 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 is to use a prototype design pattern. 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 cells one by one. Given that new cells are created from the original cell in the process of cell division, it can be said that the original cell acts as a prototype.

Prototype design pattern applications

Applying a prototype design pattern to software design will reduce the coding needed. 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, the constructor function will no longer be executed in copying objects when copying objects. 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 prevents the construction of a new object. This technique has many advantages in object-oriented programming. As you can see, implementing this pattern design is very simple, unlike other design patterns.