JAVA PROGRAMMING: PROTOTYPE DESIGN PATTERN Wrote a prototype() , or partialCopy(
ID: 3829999 • Letter: J
Question
JAVA PROGRAMMING: PROTOTYPE DESIGN PATTERN
Wrote a prototype(), or partialCopy() method for the class below. It's your choice which instance variable values are default and which are taken from the implicit parameter. A correct implementation will have these characteristics:
A. The prototyping method will take no explicit parameters.
B. The prototyping method will contain a call to construct a new instance of the desired kind of object.
C. The prototyping method will accept default instance variable values based on the object returned from the class constructor.
D. The prototyping method will assign values to a subset of the instance variables of the object to be returned based on the instance variable values of the implicit parameter object that prototyping was called on.
E. The prototyping method will return a reference to the newly created object based on the implicit parameter prototype.
Write a test program that demonstrates the use and outcome of using the method. In other words, construct an object and then call prototype on it. Graphically illustrating the outcome of prototyping can be accomplished by calling a toString() method to show the contents of the original, prototype object, and the partial copy returned by the prototyping method.
Explanation / Answer
import java.util.HashMap;
import java.util.Map;
public class Factory {
public static final Map<String, Food> prototypes = new HashMap<>();
static {
prototypes.put("RICE", new Rice());
prototypes.put("PIZZA", new Pizza());
prototypes.put("BURGER", new Burger());
}
public static Food getPrototype(String type) {
try {
return prototypes.get(type).clone();
} catch (NullPointerException ex) {
System.out.println("Prototype with name: " + type + ", doesn't exist");
return null;
}
}
public static void main(String args[]) {
Food prototype = Factory.getPrototype("RICE");
System.out.println(prototype.toString());
prototype = Factory.getPrototype("BURGER");
System.out.println(prototype.toString());
prototype = Factory.getPrototype("PIZZA");
System.out.println(prototype.toString());
}
}
abstract class Food {
private String brand;
private String productName;
private String units;
public Food(String brandIn, String productNameIn, String unitsIn) {
brand = brandIn;
productName = productNameIn;
units = unitsIn;
}
public void setBrand(String brandIn) {
brand = brandIn;
}
public String getBrand() {
return brand;
}
public void setProductName(String productNameIn) {
productName = productNameIn;
}
public String getProductName() {
return productName;
}
public void setUnits(String unitsIn) {
units = unitsIn;
}
public String getUnits() {
return units;
}
public String toString() {
String returnString = getClass().getName() + "[brand=" + brand + ", name=" + productName + ", units=" + units
+ "]";
return returnString;
}
public abstract Food clone();
}
class Rice extends Food {
public Rice() {
super("Indian Rice", "Rice", "Kgs");
}
@Override
public Food clone() {
return new Rice();
}
}
class Pizza extends Food {
public Pizza() {
super("Dominos", "Latino Pizza", "none");
}
@Override
public Food clone() {
return new Pizza();
}
}
class Burger extends Food {
public Burger() {
super("Mexicano", "Cheese Burger", "none");
}
@Override
public Food clone() {
return new Burger();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.