这里给出一个原始模型模式的例子。孙大圣的身外身法术
浅复制
设计图如下:
TheGreatestSage类扮演客户角色。
TheGreatestSage类
代码语言:javascript复制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类
代码语言:javascript复制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类
代码语言:javascript复制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类
代码语言:javascript复制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
代码语言:javascript复制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
代码语言:javascript复制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