Prototyping is a creative design pattern that allows you to duplicate existing objects without making your classes dependent on them.
Features: Object Copying: Create a new object by duplicating an existing object instead of building it from scratch. Reduced creation overhead: Frequent use of constructors to create objects is avoided, improving the efficiency of creating objects. Flexibility: Prototypes can be added or removed on the fly, allowing the runtime to decide which object type to use. Use case: Create expensive objects: When the process of creating objects is complex and time-consuming, you can use the prototype pattern to avoid repetitive initialization efforts. Categories are fixed, but objects are varied: When the type of object you need to create is fixed, but there are multiple variations in the properties or combinations of objects, you can use the prototype pattern to build a new object. Avoid inheriting to create subclasses: When you need to dynamically determine the type of an object at runtime, using the prototype pattern avoids creating a large number of subclasses. Let's say you have an object, and you want to create an exact copy of it. What would you do?First, you have to create a new object of the same class. You must then iterate through all the fields of the original object and copy their values to the new object.
But there's a catch. Not all objects can be copied in this way, as some of the object's fields may be private and not visible from the outside of the object itself. There is also a problem with the direct replication method. Since you must know the class of the object in order to create duplicates, your class will depend on that class. The prototype pattern delegates the cloning process to the cloned object itself. This pattern declares a common interface for all objects that support cloning. This interface allows you to clone an object without coupling ** to the object's class. Typically, such an interface contains only one clone method.
The implementation of the clone method is very similar across all classes. This method creates an object of the current class and transfers all the field values of the old object to the new object. You can even copy private fields, as most programming languages allow objects to access private fields of other objects that belong to the same class.
Objects that support cloning are called prototypes. When your objects have dozens of fields and hundreds of possible configurations, cloning them may be an alternative to subclassing.
The prototype class must define an alternative constructor that accepts an object of that class as a parameter. The constructor must copy the values of all fields defined in the class from the passed object to the newly created instance. If you want to change a subclass, you must call the parent constructor to let the superclass handle the clone of its private field. The Prototype Registry provides an easy way to access commonly used prototypes. It stores a set of prebuilt objects that are ready to be replicated. The simplest prototype registry is the name Prototype Hash Diagram.
To put it simply, it provides a clone interface, and the copy constructor is called in the interface, but it should be noted that for member variables such as pointers, the copy constructor needs to be overloaded to implement deep copy.
using std::string;prototype design pattern
intent: lets you copy existing objects without **your code dependent on
their classes.
enum type
prototype(string prototype_name)
prototype_name_(prototype_name)
virtual ~prototype()
virtual prototype *clone() const = 0;
virtual void method(float prototype_field)
concreteprototype1 is a sub-class of prototype and implement the clone method
in this example all data members of prototype class are in the stack. if you
h**e pointers in your properties for ex: string* name_ ,you will need to
implement the copy-constructor to make sure you h**e a deep copy from the
clone method
class concreteprototype1 : public prototype
notice that clone method return a pointer to a new concreteprototype1
replica. so, the client (who call the clone method) has the responsability
to free that memory. i you h**e smart pointer knowledge you may prefer to
use unique_pointer here.
prototype *clone() const override
class concreteprototype2 : public prototype
prototype *clone() const override
in prototypefactory you h**e two concrete prototypes, one for each concrete
prototype class, so each time you want to create a bullet , you can use the
existing ones and clone those.
class prototypefactory
be carefull of free all memory allocated. again, if you h**e smart pointers
knowelege will be better to use it here.
prototypefactory()
notice here that you just need to specify the type of the prototype you
want and the method will create from the object with this type.
prototype *createprototype(type type)
void client(prototypefactory &prototype_factory) ")
Create a prototype object.
original = prototype(42)
Use the prototype pattern for object copying.
clone1 = original.clone()
clone2 = original.clone()
Modify the value of the copied object.
clone1.value = 100
Print the information.
original.display() output: value: 42
clone1.display() output: value: 100
clone2.display() output: value: 42