I am trying to understand the Prototype desgining pattern.
Here is the sample code.
public class App {
public static void main(String[] args) {
//Prototype proto = new Prototype();
Dog d = new Dog();
Dog clone ;//= (Dog)proto.getClone(d);
clone = (Dog) d.makeCopy();
System.out.println(d);
System.out.println(clone);
System.out.println("xxxxxxxxxxxxxx");
//System.out.println();
//d.setval("test");
//System.out.println(clone.getVal());
}
}
It is clear how a clone of dog is created in a prototype.
Dog d = new Dog();
Dog clone ;
clone = (Dog) d.makeCopy();
But without doing all these steps, can't we simply do this?
Dog d = new Dog();
Dog clone = d;
The only usage I can think of is dynamically creating clone objects through Prototype as we can pass any subclass of superclass of Dog type.
Am I correct? Or is there anything else that needs to add to this?
Aucun commentaire:
Enregistrer un commentaire