Complete the code to declare the Prototype interface method for cloning.
interface Prototype {
Prototype [1]();
}The standard method name for creating a copy in the Prototype pattern is clone().
Complete the code to implement the clone method in a concrete prototype class.
class ConcretePrototype implements Prototype { private int id; ConcretePrototype(int id) { this.id = id; } public Prototype [1]() { return new ConcretePrototype(this.id); } }
The clone() method returns a new instance copying the current object's state.
Fix the error in the clone method to correctly copy the object state.
public Prototype clone() {
return new ConcretePrototype([1]);
}The clone method must use this.id to copy the current object's id.
Fill both blanks to create a prototype registry and clone an object from it.
class PrototypeRegistry { private Map<String, Prototype> prototypes = new HashMap<>(); public void addPrototype(String key, Prototype prototype) { prototypes.put([1], [2]); } public Prototype getClone(String key) { return prototypes.get(key).clone(); } }
Use key as the map key and prototype as the value to store the prototype object.
Fill all three blanks to clone a prototype and modify its state.
Prototype original = registry.getClone([1]); ConcretePrototype copy = (ConcretePrototype) original; copy.[2] = [3];
Clone the prototype with key "item1", then set its id to 42.
