Skip to content

Latest commit

 

History

History

prototype

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Prototype

Lets you copy existing objects without making your dependent on their classes.

Examples

How to Implement

  1. Create the prototype interface and declare the clone method in it. Or just add the method to all classes of an existing class heirarchy, if you have one.
  2. A prototype class must define the alternative constructor that accepts an object of that class as an argument. The constructor must copy the values of all fields defined in the class from the passed object into the newly created instance. If you’re changing a subclass, you must call the parent constructor to let the superclass handle the cloning of its private fields.
  3. The cloning method usually consists of just one line: running a new operator with the prototypical version of the constructor. Note, that every class must explicitly override the cloning method and use its own class name along with the new operator. Otherwise, the cloning method may produce an object of a parent class.
  4. Optionally, create a centralized prototype registry to store a catalog of frequently used prototypes.

Prototype