赞
踩
import java.util.Date; /** * 苹果基类(不是必须写) * * @author ALion * @version 2019/1/28 22:56 */ public class Apple implements Cloneable { protected int size; protected String place; protected Date manufacture; public Apple(int size, String place, Date manufacture) { this.size = size; this.place = place; this.manufacture = manufacture; } }
import java.util.Date; /** * 可克隆的苹果(浅克隆) * <p> * 1.需要实现Cloneable接口 * 2.覆写Object的clone方法 * </p> * * @author ALion * @version 2019/1/28 22:21 */ public class ShallowApple extends Apple { public ShallowApple(int size, String place, Date manufacture) { super(size, place, manufacture); } @Override protected ShallowApple clone() throws CloneNotSupportedException { // clone是native方法,直接调用C代码,效率高 return (ShallowApple) super.clone(); } }
import java.util.Date; /** * 可克隆的苹果(深克隆) * <p> * 1.需要实现Cloneable接口 * 2.覆写Object的clone方法,同时把属性也克隆 * </p> * * @author ALion * @version 2019/1/28 22:21 */ public class DeepApple extends Apple { public DeepApple(int size, String place, Date manufacture) { super(size, place, manufacture); } @Override protected DeepApple clone() throws CloneNotSupportedException { // clone是native方法,直接调用C代码,效率高 DeepApple apple = (DeepApple) super.clone(); // 深克隆需要同时把属性对象也克隆 apple.manufacture = (Date) this.manufacture.clone(); return apple; } }
import java.io.Serializable; import java.util.Date; /** * 可序列化的苹果(深克隆) * * @author ALion * @version 2019/1/28 22:21 */ public class SerializableApple implements Serializable { public int size; public String place; public Date manufacture; public SerializableApple(int size, String place, Date manufacture) { this.size = size; this.place = place; this.manufacture = manufacture; } }
/** * 家庭作业类(用于测试原型模式性能) * <p> * 1.自己做家庭作业很慢 * 2.直接抄(克隆)别人的很快 * </p> * * @author ALion * @version 2019/1/28 23:37 */ public class Homework implements Cloneable{ public Homework() { try { // 完成家庭作业需要一定时间 Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } @Override protected Homework clone() throws CloneNotSupportedException { return (Homework) super.clone(); } }
import java.util.Date; /** * 浅克隆测试 * * @author ALion * @version 2019/1/28 23:12 */ public class ShallowCloneTest { public static void main(String[] args) throws CloneNotSupportedException { System.out.println("------+--浅克隆Test--+------"); ShallowApple apple = new ShallowApple(10, "China", new Date()); print(apple); System.out.println("------+------+------"); ShallowApple cloneApple = apple.clone(); print(cloneApple); System.out.println("修改manufacture"); apple.manufacture.setTime(10000000000L); System.out.println("apple.manufacture = " + apple.manufacture); System.out.println("cloneApple.manufacture = " + cloneApple.manufacture); } private static void print(Apple apple) { System.out.println("apple = " + apple); System.out.println("apple.size = " + apple.size); System.out.println("apple.place = " + apple.place); System.out.println("apple.manufacture = " + apple.manufacture); } }
import java.util.Date; /** * 深克隆测试 * * @author ALion * @version 2019/1/28 22:32 */ public class DeepCloneTest { public static void main(String[] args) throws CloneNotSupportedException { System.out.println("------+--深克隆Test--+------"); DeepApple apple = new DeepApple(10, "China", new Date()); print(apple); System.out.println("------+------+------"); DeepApple cloneApple = apple.clone(); print(cloneApple); System.out.println("修改manufacture"); apple.manufacture.setTime(10000000000L); System.out.println("apple.manufacture = " + apple.manufacture); System.out.println("cloneApple.manufacture = " + cloneApple.manufacture); } private static void print(Apple apple) { System.out.println("apple = " + apple); System.out.println("apple.size = " + apple.size); System.out.println("apple.place = " + apple.place); System.out.println("apple.manufacture = " + apple.manufacture); } }
import java.io.*; import java.util.Date; /** * 序列化、反序列化的方式实现深克隆 测试 * * @author ALion * @version 2019/1/28 23:20 */ public class SerializableCloneTest { public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("------+--深克隆Test--+------"); SerializableApple apple = new SerializableApple(10, "China", new Date()); print(apple); System.out.println("------+------+------"); // 序列化 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(apple); byte[] bytes = bos.toByteArray(); // 反序列化 ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); SerializableApple cloneApple = (SerializableApple) ois.readObject(); print(cloneApple); System.out.println("修改manufacture"); apple.manufacture.setTime(10000000000L); System.out.println("apple.manufacture = " + apple.manufacture); System.out.println("cloneApple.manufacture = " + cloneApple.manufacture); } private static void print(SerializableApple apple) { System.out.println("apple = " + apple); System.out.println("apple.size = " + apple.size); System.out.println("apple.place = " + apple.place); System.out.println("apple.manufacture = " + apple.manufacture); } }
/** * 原型模式的性能测试 * <p> * new生成对象太慢时or同时间需要创建大量相同对象时,就使用原型模式 * </p> * * @author ALion * @version 2019/1/28 23:34 */ public class PerformanceTest { public static void main(String[] args) throws CloneNotSupportedException { // 直接做家庭作业 doHomework(); // 抄(克隆)他人的作业 cloneHomework(); } public static void doHomework() { long start = System.currentTimeMillis(); Homework homework = new Homework(); for (int i = 0; i < 100; i++) { Homework homework2 = new Homework(); System.out.println("do homework = " + i); } long end = System.currentTimeMillis(); System.out.println("PerformanceTest.doHomework: 耗时" + (end - start) + "毫秒"); } public static void cloneHomework() throws CloneNotSupportedException { long start = System.currentTimeMillis(); Homework homework = new Homework(); for (int i = 0; i < 100; i++) { Homework homework2 = homework.clone(); System.out.println("clone homework = " + i); } long end = System.currentTimeMillis(); System.out.println("PerformanceTest.cloneHomework: 耗时" + (end - start) + "毫秒"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。