java23设计模式原型模式
浅克隆

package com.bjsxt.prototype; import java.io.Serializable; import java.util.Date; public class Sheep implements Cloneable,Serializable { //1997,英国的克隆羊,多利! private String sname; private Date birthday; @Override protected Object clone() throws CloneNotSupportedException { Object obj = super.clone(); //直接调用object对象的clone()方法! return obj; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Sheep(String sname, Date birthday) { super(); this.sname = sname; this.birthday = birthday; } public Sheep() { } }

package com.bjsxt.prototype; import java.util.Date; /** * 测试原型模式(浅克隆) * @author 尚学堂高淇 www.sxt.cn * */ public class Client { public static void main(String[] args) throws Exception { Date date = new Date(12312321331L); Sheep s1 = new Sheep("少利",date); System.out.println(s1); System.out.println(s1.getSname()); System.out.println(s1.getBirthday()); Sheep s2 = (Sheep) s1.clone(); date.setTime(23432432423L); System.out.println(s1.getBirthday()); s2.setSname("多利"); System.out.println(s2); System.out.println(s2.getSname()); System.out.println(s2.getBirthday()); /** * com.bjsxt.prototype.Sheep@42a57993 * 少利 * Sat May 23 20:05:21 CST 1970 * Tue Sep 29 13:00:32 CST 1970 * com.bjsxt.prototype.Sheep@74a14482 * 多利 * Tue Sep 29 13:00:32 CST 1970 */ } }
深克隆

package com.bjsxt.prototype; import java.util.Date; //测试深复制 public class Sheep2 implements Cloneable { //1997,英国的克隆羊,多利! private String sname; private Date birthday; @Override protected Object clone() throws CloneNotSupportedException { Object obj = super.clone(); //直接调用object对象的clone()方法! //添加如下代码实现深复制(deep Clone) Sheep2 s = (Sheep2) obj; s.birthday = (Date) this.birthday.clone(); //把属性也进行克隆! return obj; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Sheep2(String sname, Date birthday) { super(); this.sname = sname; this.birthday = birthday; } public Sheep2() { } }

package com.bjsxt.prototype; import java.util.Date; /** * 原型模式(深复制) * @author 尚学堂高淇www.sxt.cn * */ public class Client2 { public static void main(String[] args) throws CloneNotSupportedException { Date date = new Date(12312321331L); Sheep2 s1 = new Sheep2("少利",date); Sheep2 s2 = (Sheep2) s1.clone(); //实现深复制。s2对象的birthday是一个新对象! System.out.println(s1); System.out.println(s1.getSname()); System.out.println(s1.getBirthday()); date.setTime(23432432423L); System.out.println(s1.getBirthday()); s2.setSname("多利"); System.out.println(s2); System.out.println(s2.getSname()); System.out.println(s2.getBirthday()); /** * com.bjsxt.prototype.Sheep2@42a57993 * 少利 * Sat May 23 20:05:21 CST 1970 * Tue Sep 29 13:00:32 CST 1970 * com.bjsxt.prototype.Sheep2@74a14482 * 多利 * Sat May 23 20:05:21 CST 1970 */ } }
深克隆 -对象流

package com.bjsxt.prototype; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; /** * 原型模式(深复制,使用序列化和反序列化的方式实现深复制) * @author 尚学堂高淇www.sxt.cn * */ public class Client3 { public static void main(String[] args) throws CloneNotSupportedException, Exception { Date date = new Date(12312321331L); Sheep s1 = new Sheep("少利",date); System.out.println(s1); System.out.println(s1.getSname()); System.out.println(s1.getBirthday()); // 使用序列化和反序列化实现深复制 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(s1); byte[] bytes = bos.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bis); Sheep s2 = (Sheep) ois.readObject(); //克隆好的对象! System.out.println("修改原型对象的属性值"); date.setTime(23432432423L); System.out.println(s1.getBirthday()); s2.setSname("多利"); System.out.println(s2); System.out.println(s2.getSname()); System.out.println(s2.getBirthday()); /** * com.bjsxt.prototype.Sheep@42a57993 * 少利 * Sat May 23 20:05:21 CST 1970 * 修改原型对象的属性值 * Tue Sep 29 13:00:32 CST 1970 * com.bjsxt.prototype.Sheep@6acbcfc0 * 多利 * Sat May 23 20:05:21 CST 1970 */ } }
分类:
java23设计模式 / 创建型
, java23设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决