设计模式~原始模型模式(二)
这里给出一个原始模型模式的例子。孙大圣的身外身法术
浅复制
设计图如下:
TheGreatestSage类扮演客户角色。
TheGreatestSage类
package com.vincent.prototype; public class TheGreatestSage { private Monkey monkey = new Monkey(); public void change(){ Monkey copyMonkey; for(int i=0;i<2000;i++){}; copyMonkey = (Monkey)monkey.clone(); System.out.println("Monkey King's birth date="+monkey.getBirthDate()); System.out.println("Copy monkey's birth date="+copyMonkey.getBirthDate()); System.out.println("Monkey King == Copy Monkey?"+(monkey==copyMonkey)); System.out.println("Monkey King's Staff==Copy Monkey's Staff?"+(monkey.getStaff()==copyMonkey.getStaff())); } public static void main(String[] args) { // TODO Auto-generated method stub TheGreatestSage sage = new TheGreatestSage(); sage.change(); } }
Monkey类
package com.vincent.prototype; import java.util.Date; public class Monkey implements Cloneable { private int height; private int weight; private GoldRingedStaff staff; private Date birthDate; /** * 构造函数 */ public Monkey(){ this.birthDate = new Date(); } public Object clone(){ Monkey temp = null; try{ temp = (Monkey)super.clone(); }catch(CloneNotSupportedException e){ System.out.println("Clone failed"); }finally{ return temp; } } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public GoldRingedStaff getStaff() { return staff; } public void setStaff(GoldRingedStaff staff) { this.staff = staff; } }
GoldRingedStaff类
package com.vincent.prototype; public class GoldRingedStaff { private float height = 100.F; private float diameter = 10.F; /** * 构造函数 */ public GoldRingedStaff(){ // write your code here } /** * 增长行为 */ public void grow(){ this.diameter *=2.0; this.height *=2.0; } /** * 缩小行为 */ public void shrink(){ this.diameter /= 2; this.height /= 2; } public void move(){ //wirte you code for moving the staff } public float getHeight() { return height; } public void setHeight(float height) { this.height = height; } public float getDiameter() { return diameter; } public void setDiameter(float diameter) { this.diameter = diameter; } }
运行结果如下:
Monkey King's birth date=Fri Aug 14 07:15:59 CST 2020
Copy monkey's birth date=Fri Aug 14 07:15:59 CST 2020
Monkey King == Copy Monkey?false
Monkey King's Staff==Copy Monkey's Staff?true
深复制
为做到深复制,所有需要复制的对象需要实现 java.io.Serializable接口。
系统设计如下:
TheGreatestSage类
package com.vincent.prototype; import java.io.IOException; public class TheGreatestSage { private Monkey monkey = new Monkey(); public void change() throws IOException,ClassNotFoundException{ Monkey copyMonkey; for(int i=0;i<2000;i++){}; copyMonkey = (Monkey)monkey.deepClone(); System.out.println("Monkey King's birth date="+monkey.getBirthDate()); System.out.println("Copy monkey's birth date="+copyMonkey.getBirthDate()); System.out.println("Monkey King == Copy Monkey?"+(monkey==copyMonkey)); System.out.println("Monkey King's Staff==Copy Monkey's Staff?"+(monkey.getStaff()==copyMonkey.getStaff())); } public static void main(String[] args) throws IOException,ClassNotFoundException{ // TODO Auto-generated method stub TheGreatestSage sage = new TheGreatestSage(); sage.change(); } }
Monkey
package com.vincent.prototype; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OptionalDataException; import java.io.Serializable; import java.util.Date; public class Monkey implements Cloneable,Serializable { private int height; private int weight; private GoldRingedStaff staff; private Date birthDate; /** * 构造函数 */ public Monkey(){ this.birthDate = new Date(); this.staff = new GoldRingedStaff(); } /** * 深克隆方法 * @return * @throws IOException * @throws OptionalDataException * @throws ClassNotFoundException */ public Object deepClone() throws IOException,OptionalDataException,ClassNotFoundException { //首先将对象写出到流里 ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(this); //然后将对象从流里读出来 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bi); return (oi.readObject()); } /** * 浅克隆方法 */ public Object clone(){ Monkey temp = null; try{ temp = (Monkey)super.clone(); }catch(CloneNotSupportedException e){ System.out.println("Clone failed"); }finally{ return temp; } } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public GoldRingedStaff getStaff() { return staff; } public void setStaff(GoldRingedStaff staff) { this.staff = staff; } }
GoldRingedStaff
package com.vincent.prototype; import java.io.Serializable; public class GoldRingedStaff implements Cloneable,Serializable{ private float height = 100.F; private float diameter = 10.F; /** * 构造函数 */ public GoldRingedStaff(){ // write your code here } /** * 增长行为 */ public void grow(){ this.diameter *=2.0; this.height *=2.0; } /** * 缩小行为 */ public void shrink(){ this.diameter /= 2; this.height /= 2; } public void move(){ //wirte you code for moving the staff } public float getHeight() { return height; } public void setHeight(float height) { this.height = height; } public float getDiameter() { return diameter; } public void setDiameter(float diameter) { this.diameter = diameter; } }
结果如下:
Monkey King's birth date=Fri Aug 14 07:37:04 CST 2020
Copy monkey's birth date=Fri Aug 14 07:37:04 CST 2020
Monkey King == Copy Monkey?false
Monkey King's Staff==Copy Monkey's Staff?false
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2019-08-14 微服务之演化式架构师(二)