使用原型实例来指定要创建对象的类型,通过复制这个原型来创建新对象。
public interface Prototype{
Prototype Clone();
}
public class ConcretePrototype extends Prototype{
private String filed;
public ConcretePrototype(String filed){
this.filed = filed;
}
@Override
Prototype Clone(){
return new ConcretePrototype(filed)
}
@Override
public String toString(){
return filed;
}
}
public class client(){
public static void main(String[] args){
Prototype prototype = new ConcretePrototype("abc");
Prototype clone = prototype.clone();
System.out.print(clone);
}
}
结果:
abc