java: Prototype Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 原始模型 Prototype Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc PandaToClone.java * * */ package com.javapatterns.prototype; import java.lang.Object.*; public class PandaToClone implements Cloneable { private int height, weight, age; public PandaToClone ( int height, int weight) { this .age = 0 ; this .weight = weight; this .height = height; } public void setAge( int age) { this .age = age; } public int getAge() { return age; } public int getHeight() { return height; } public int getWeight() { return weight; } public Object clone() { PandaToClone temp = new PandaToClone(height, weight); temp.setAge(age); return (Object) temp; } } |
调用测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //原始模型 thisPanda = new PandaToClone( 15 , 25 ); thisPanda.setAge( 3 ); // Create the second object by cloning the first thatPanda = (PandaToClone) thisPanda.clone(); // Now describe these objects on the system console : System.out.println( " Age of this panda : " + thisPanda.getAge()); System.out.println( " height : " + thisPanda.getHeight()); System.out.println( " weight : " + thisPanda.getWeight()); System.out.println( " Age of that panda : " + thatPanda.getAge()); System.out.println( " height : " + thatPanda.getHeight()); System.out.println( " weight : " + thatPanda.getWeight()); |
输出:
1 2 3 4 5 6 | Age of this panda : 3 height : 15 weight : 25 Age of that panda : 3 height : 15 weight : 25 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 原始模型 Prototype Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Monkey.java * * */ package com.javapatterns.prototype; import java.util.Date; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.IOException; import java.io.OptionalDataException; import java.io.Serializable; /** * 大圣,猢狲 * * */ 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(); } /** *深克隆方法 * * */ public Object deepClone() throws IOException, OptionalDataException, ClassNotFoundException { //write to stream ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject( this ); //read from stream 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 原始模型 Prototype Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc GoldRingedStaff.java * * */ package com.javapatterns.prototype; import java.util.Date; import java.io.Serializable; /** * 金箍棒 * * */ public class GoldRingedStaff implements Cloneable, Serializable{ private float height = 100 .0F; private float diameter = 10 .0F; /** * * */ public GoldRingedStaff() { //write your code here } /** * * */ public void grow() { this .diameter *= 2.0 ; this .height *= 2 ; } /** * * */ public void shrink() { this .diameter /= 2 ; this .height /= 2 ; } /** * * */ public void move() { //write your 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 原始模型 Prototype Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc TheGreatestSage.java * * */ package com.javapatterns.prototype; import java.util.Date; import java.io.IOException; import java.lang.ClassNotFoundException; /** * 大圣 * */ public class TheGreatestSage { private Monkey monkey = new Monkey(); public TheGreatestSage() { } /** * * * */ public void change() throws IOException, ClassNotFoundException { try { 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())); } catch (ClassNotFoundException cex) { cex.printStackTrace(); } catch (IOException ioException) { ioException.printStackTrace(); } } } |
调用测试:
1 2 3 4 5 6 7 8 9 10 | try { TheGreatestSage theGreatestSage= new TheGreatestSage(); theGreatestSage.change(); } catch (Exception exception) { exception.printStackTrace(); } |
输出:
1 2 3 4 | Monkey King's birth date=Fri Sep 16 22 : 08 : 46 CST 2022 Copy monkey's birth date=Fri Sep 16 22 : 08 : 46 CST 2022 Monkey King == Copy Monkey? false Monkey King 's Staff == Copy Monkey' s Staff? false |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2010-09-16 Csharp GridView Sorting 字段排序用法