A fully initialized instance to be copied or cloned. Specify the kinds of
objects to create using a prototypical instance, and create new objects
by copying this prototype.
There are two advantages of clone. 1)It can reduces complex and growing
class contruction time. 2)Sometimes, you don’t know what kind of object
should be created, For example, passing an abstract product obj pionter ,
at this time, you don’t know what kind of child class it pointed.
You can use RTTI, But a better method is to use clone() directly.
In C#, base class object include clone() method. it just use binary stream
way to clone a object. In Java, you can extend IClone interface.
clone is so important, it become a "build-in" feature in Java or C#
language.
In C++, you can just add clone method in your class if you want to it
support clone. You don’t need to create a baseClass with virtual clone(),
then inheriate you present class from it.
But in a music note example, There are HalfNote and WholeNote classes,
and dragMusicNote() need to support them all. At this time, you need to
create a prototype baseClass, and make you HalfNote and WholeNote
derived from it. Just like previous figure show.
MusicNote give us a good hint. Inheritance is a 1) is-a relationship, 2)You also can use it when it subclass and baseclass both supportsome actions. such as clone().
Inside clone(), you can use new return a pointer, or use(*this) return a
value.