赞
踩
原型模式是一种创建型设计模式, 用于创建对象, 而不必指定它们所属的具体类. 它通过复制现有对象 (即原型) 来创建新对象. 原型模式适用于当创建新对象的过程代价较高或复杂时, 通过克隆现有对象来提高性能
// 原型接口 interface Prototype { Prototype clone(); } // 具体原型类 class ConcretePrototype implements Prototype { private String field; public ConcretePrototype(String field) { this.field = field; } // 实现克隆方法 @Override public Prototype clone() { return new ConcretePrototype(this.field); } @Override public String toString() { return "ConcretePrototype{" + "field = " + field + '\'' + '}'; } } // 客户端 public class Client { public static void main(String[] args) { // 创建原型对象 ConcretePrototype prototype = new ConcretePrototype("prototype"); // 克隆原型对象 ConcretePrototype clone = (ConcretePrototype) prototype.clone(); System.out.println(prototype); System.out.println(clone); } }
在原型模式中, 克隆可以分为浅拷贝和深拷贝
下面是深拷贝和浅拷贝的实例
浅拷贝:
import java.util.ArrayList; import java.util.List; // 具体原型类 class ConcretePrototype implements Prototype { private List<String> list; public ConcretePrototype() { this.list = new ArrayList<>(); } public void addItem(String item) { this.list.add(item); } @Override public Prototype clone() { try { return (ConcretePrototype) super.clone(); // 浅拷贝 } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } @Override public String toString() { return "ConcretePrototype{" + "list=" + list + '}'; } }
深拷贝:
import java.util.ArrayList; import java.util.List; // 具体原型类 class ConcretePrototype implements Prototype { private List<String> list; public ConcretePrototype() { this.list = new ArrayList<>(); } public void addItem(String item) { this.list.add(item); } @Override public Prototype clone() { ConcretePrototype clone = new ConcretePrototype(); clone.list = new ArrayList<>(this.list); // 深拷贝 return clone; } @Override public String toString() { return "ConcretePrototype{" + "list=" + list + '}'; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。