原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式其实就是从一个
/** * @Author: subd * @Date: 2019/9/19 8:27 * 原型模式 */ public class PrototypeModel { /** * 原型类 */ static abstract class Prototype { private String id; public Prototype(String id) { this.id = id; } public String getId() { return id; } public abstract Prototype Clone() throws CloneNotSupportedException; } /** * 具体原型类 */ static class concretePrototype1 extends Prototype { public concretePrototype1(String id) { super(id); } @Override public Prototype Clone() throws CloneNotSupportedException { return (Prototype) this.clone(); } } public static void main(String[] args) throws CloneNotSupportedException { concretePrototype1 p1 = new concretePrototype1("p1"); concretePrototype1 c1 = (concretePrototype1) p1.Clone(); System.out.println(c1.getId()); } }
对象再创建另外一个可定制的对象。而且不需知道任何创建的细节。