1.简介
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象,原型模式创建对象不需要知道任何创建的细节。在java中由于提供了clone()方法,所以java中的原型模式简单了许多。
2.示例
//原型类
public class Prototype implements Cloneable { | |
private String name; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Prototype clone()throws CloneNotSupportedException { | |
return (Prototype)super.clone(); | |
} | |
} |
public class Main { | |
public static void main(String[] args) throws Exception{ | |
// TODO Auto-generated method stub | |
Prototype vo = new Prototype(); | |
for(int i=0;i<10;i++){ | |
vo.clone(); | |
} | |
} | |
} |