原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
// 原型接口 public interface Prototype { Prototype clone(); } // 具体原型类 public class ConcretePrototype implements Prototype { private String name; public ConcretePrototype(String name) { this.name = name; } @Override public Prototype clone() { return new ConcretePrototype(this.name); } public String getName() { return name; } } // 使用示例 public class Client { public static void main(String[] args) { ConcretePrototype prototype = new ConcretePrototype("原始对象"); ConcretePrototype clone = (ConcretePrototype) prototype.clone(); System.out.println("克隆对象名称: " + clone.getName()); } }