就是一个现成的对象,这个对象已有设计好的值
当我要新建一个对象,并且要给新对象赋一样的值
注意:原型模式不走构造方法
一
1.浅拷贝
能够直接拷贝的数据类型只有9中,java的基本数据类型+string,list、map等的拷贝是引用地址,浅拷贝,如果要深拷贝,必须让list也去clone
import java.util.ArrayList;
import java.util.List;
public class ConcretePrototype extends Prototype{
private int age;
public List<String> list =new ArrayList<String>();
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Prototype implements Cloneable{
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
ConcretePrototype cp=new ConcretePrototype();
cp.setAge(18);
cp.list.add("1");
try {
ConcretePrototype copy=(ConcretePrototype) cp.clone();
System.out.println(copy.getAge());
System.out.println(cp==copy);//不是同一个对象
System.out.println(cp.list==copy.list);//list指向同一个地址,不合理,浅拷贝
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
//能够直接拷贝的数据类型只有9中,java的基本数据类型+string,list、map等的拷贝是引用地址,浅拷贝,如果要深拷贝,必须让list也去clone
2.深拷贝
import java.util.ArrayList;
public class Prototype implements Cloneable{
public ArrayList<String> list =new ArrayList<String>();
@SuppressWarnings("unchecked")
protected Object clone() throws CloneNotSupportedException {
Prototype prototype=null;
prototype=(Prototype) super.clone();
prototype.list=(ArrayList<String>) list.clone();
return prototype;
}
}
public class ConcretePrototype extends Prototype{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
ConcretePrototype cp=new ConcretePrototype();
cp.setAge(18);
cp.list.add("1");
try {
ConcretePrototype copy=(ConcretePrototype) cp.clone();
System.out.println(copy.getAge());
System.out.println(cp==copy);//不是同一个对象
System.out.println(cp.list==copy.list);//list指向不同地址,深拷贝
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
3.如果是list里包含map,map包含list,就要用循环或者反射了
二
1.浅拷贝
import java.util.Date;
public class Monkey {
protected int height;
protected int weight;
protected Date birthday;//不是基本类型,默认浅拷贝
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 getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
import java.util.Date;
//孙悟空
public class SunWuKong extends Monkey implements Cloneable{
//金箍棒
private JinGuBang bang;
//构造方法,从石头蹦出来
public SunWuKong() {
this.bang=new JinGuBang();
this.birthday=new Date();
this.height=150;
this.weight=30;
}
//分身 技能
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
//变化
public void change() {
SunWuKong copySun=(SunWuKong) clone();
System.out.println("大圣本尊和克隆的生日是否为同一对象:"+(this.getBirthday().getTime()==copySun.getBirthday().getTime()));
System.out.println("大圣本尊和克隆是否为同一对象:"+(this==copySun));
System.out.println("大圣本尊持有的金箍棒和克隆大圣持有金箍棒是否为同一个对象"+(this.getBang()==copySun.getBang()));
}
public JinGuBang getBang() {
return bang;
}
public void setBang(JinGuBang bang) {
this.bang = bang;
}
}
import java.io.Serializable;
//金箍棒
public class JinGuBang implements Serializable{
private float height=100;
private float diameter=10;
/**
* 金箍棒长大
*/
public void grow() {
this.diameter *=2;
this.height *=2;
}
/**
* 金箍棒縮小
*/
public void shrink() {
this.diameter /=2;
this.height /=2;
}
}
public class Main {
public static void main(String[] args) {
SunWuKong sunWuKong=new SunWuKong();
sunWuKong.change();
}
}
2.深拷贝
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
//孙悟空
public class SunWuKong extends Monkey implements Cloneable,Serializable{
//金箍棒
private JinGuBang bang;
//构造方法,从石头蹦出来
public SunWuKong() {
this.bang=new JinGuBang();
this.birthday=new Date();
this.height=150;
this.weight=30;
}
//分身 技能
public Object clone() {
ByteArrayOutputStream bos=null;
ObjectOutputStream oos=null;
ByteArrayInputStream bis=null;
ObjectInputStream ois=null;
try {
bos=new ByteArrayOutputStream();
oos=new ObjectOutputStream(bos);
oos.writeObject(this);
bis=new ByteArrayInputStream(bos.toByteArray());
ois=new ObjectInputStream(bis);
SunWuKong copy=(SunWuKong) ois.readObject();
copy.birthday=new Date();
return copy;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//变化
public void change() {
SunWuKong copySun=(SunWuKong) clone();
System.out.println("大圣本尊和克隆的生日是否为同一对象:"+(this.getBirthday().getTime()==copySun.getBirthday().getTime()));
System.out.println("大圣本尊和克隆是否为同一对象:"+(this==copySun));
System.out.println("大圣本尊持有的金箍棒和克隆大圣持有金箍棒是否为同一个对象"+(this.getBang()==copySun.getBang()));
}
public JinGuBang getBang() {
return bang;
}
public void setBang(JinGuBang bang) {
this.bang = bang;
}
}