简单形式的原始模型模式Prototype
/**
* 抽象原型角色
*/
public interface Apple extends Cloneable {
public Apple clone();
public String getName();
public void setName(String name);
public String getColor();
public void setColor(String color);
}
/**
* 具体原型角色
*/
public class ApplePrototype implements Apple {
private String name;
private String color;
@Override
public Apple clone() {
try {
return (Apple) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String getColor() {
return color;
}
@Override
public void setColor(String color) {
this.color = color;
}
}
/**
* 客户角色
*/
public class AppleClient {
public static void main(String[] args) {
Apple apple = new ApplePrototype();
apple.setName("富士");
apple.setColor("红色");
Apple fushi = apple.clone();
System.out.println(apple.getName() + " " + apple.getColor());
System.out.println(fushi.getName() + " " + fushi.getColor());
}
}