当前位置: 代码迷 >> 综合 >> java设计模式学习总结--原型模式
  详细解决方案

java设计模式学习总结--原型模式

热度:5   发布时间:2024-02-09 18:09:14.0

原型模式(Prototype Partten)

   定义:以现有的某个实例为原型,通过复制原型获得更多相同或相似的对象。

   特点:

  1. 通过clone生成新对象;
  2. 可以跳过一系列构建条件,对象生成较为便捷快速;

   实现:

     原型模式是最创建型设计模式,以现有的实例为原型,通过复制,高效快速地生成新对象,特别是在一些需要生成大量对象的场景(将大量数据库数据映射成实体对象时),使用原型模式生成对象要比new的方式更为高效。原型模式的克隆分为浅克隆和深克隆,在java中原型类直接实现Cloneable 接口就可实现对象的克隆

  1.浅克隆

package design.prototype;import java.util.Arrays;public class Prototype implements Cloneable {private String name;private String[] strs;public String[] getStrs() {return strs;}public void setStrs(String[] strs) {this.strs = strs;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overrideprotected Object clone() throws CloneNotSupportedException {Prototype ptt = (Prototype) super.clone();ptt.setName(this.name);ptt.setStrs(this.strs);return ptt;}public void print() {System.out.println(this.name + "--" + Arrays.toString(this.strs)+"--"+this.strs.toString());}}

2.深克隆

package design.prototype;import java.util.Arrays;public class Prototype implements Cloneable {private String name;private String[] strs;public String[] getStrs() {return strs;}public void setStrs(String[] strs) {this.strs = strs;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overrideprotected Object clone() throws CloneNotSupportedException {Prototype ptt = (Prototype) super.clone();ptt.setName(this.name);ptt.setStrs(this.strs.clone());return ptt;}public void print() {System.out.println(this.name + "--" + Arrays.toString(this.strs)+"--"+this.strs.toString());}}

 

package design.prototype;public class Test {public static void main(String[] args) {Prototype p1 = new Prototype();String[] t = { "1", "1" };p1.setName("l");p1.setStrs(t);Prototype p2;try {p2 = (Prototype) p1.clone();p1.print();p2.print();// 对p2.进行修改String[] t1 = p2.getStrs();t1[0] = "222";p2.setStrs(t1);p1.print();p2.print();} catch (CloneNotSupportedException e) {e.printStackTrace();}}}

用上面的Test.java进行测试:

1.浅克隆下的输出:

l--[1, 1]--[Ljava.lang.String;@15db9742
l--[1, 1]--[Ljava.lang.String;@15db9742
l--[222, 1]--[Ljava.lang.String;@15db9742
l--[222, 1]--[Ljava.lang.String;@15db9742

2.深克隆下的输出:

l--[1, 1]--[Ljava.lang.String;@15db9742
l--[1, 1]--[Ljava.lang.String;@6d06d69c
l--[1, 1]--[Ljava.lang.String;@15db9742
l--[222, 1]--[Ljava.lang.String;@6d06d69c

         可以看到,在浅克隆中对于数组strs 的克隆只是将数组地址复制到了新对象中,并未创建新的数组,也就是说浅克隆只克隆你指定的对象,至于你指定的对象内部的对象,它不克隆,还是把引用给你,共享变量。在使用时需注意,如果需要克隆指定的对象内部的对象,那就需要在clone方法内,实例化内部对象或者调用内部对象的clone方法