目录
1、例子

public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("tom", 1, "白色");
Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep2 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep3 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
Sheep sheep4 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
}
}
2、概述(类封装一个拷贝的方法)
用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象。
原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节
工作的原理是:通过将一个原先对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。即;对象.clone()
public class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep() {
}
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
/**
* 克隆该实例,使用默认的clone方法
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone(){
Sheep sheep=null;
try
{
sheep=(Sheep) super.clone();
}catch(Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
}
public class Client {
public static void main(String[] args) {
System.out.println("使用原型模式");
Sheep sheep = new Sheep("tom", 1, "白色");
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
Sheep sheep4 = (Sheep) sheep.clone();
Sheep sheep5 = (Sheep) sheep.clone();
System.out.println(sheep);
System.out.println(sheep4);
System.out.println(sheep2);
System.out.println(sheep3);
}
}
3、原型模式-浅拷贝
除基本数据类型,浅拷贝 后的其他数据类型变量都执行同一个内存地址。因为浅拷贝只复制引用指针给另外一个对象
BeanUtils.copyProperties //不能拷贝对象,拷贝后对象变量为null
3.1、对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象
3.2、对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组,某个类的对象,那么浅拷贝会进行引用传递,也就是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个实例。这种情况下。一个对象中修改成员变量会影响到另一个对象的成员变量
默认的clone()就是浅拷贝
public class Sheep implements Cloneable{
private String name;
private int age;
private String color;
public Sheep friend;
public Sheep() {
}
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"name='" + name + '\'' +
", age=" + age +
", color='" + color + '\'' +
'}';
}
/**
* 克隆该实例,使用默认的clone方法
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone(){
Sheep sheep=null;
try
{
sheep=(Sheep) super.clone();
}catch(Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
}
public class Client {
public static void main(String[] args) {
System.out.println("使用原型模式");
Sheep sheep = new Sheep("tom", 1, "白色");
sheep.friend=new Sheep("AA",2,"CC");
Sheep sheep2 = (Sheep) sheep.clone();
Sheep sheep3 = (Sheep) sheep.clone();
Sheep sheep4 = (Sheep) sheep.clone();
Sheep sheep5 = (Sheep) sheep.clone();
System.out.println(sheep+"friend="+sheep.friend.hashCode());
System.out.println(sheep4+"friend4="+sheep4.friend.hashCode());
System.out.println(sheep2+"friend2="+sheep2.friend.hashCode());
System.out.println(sheep3+"friend3="+sheep3.friend.hashCode());
System.out.println(sheep.friend.equals(sheep2.friend));
}
}
4、原型模式-深拷贝
4.1、复制对象的所有基本数据类型的成员变量值
4.2、为所有引用数据类型的成员变量申请存储空间(在堆内存),并复制每个引用数据类型成员变量所引用的对象,直到该对象可达到所有对象,也就是说,对象进行深拷贝要对整个对象进行拷贝
4.3、深拷贝实现1:重写clone方法来实现深拷贝
4.4、深拷贝实现2:通过对象序列化实现深拷贝(推荐)
public class DeepProtoType implements Serializable, Cloneable {
public String name; //String 属性
public DeepCloneableTarget deepCloneableTarget;// 引用类型
public DeepProtoType() {
super();
}
@Override
public String toString() {
return "DeepProtoType{" +
"name='" + name + '\'' +
", deepCloneableTarget=" + deepCloneableTarget +
'}';
}
//深拷贝-方式1 使用clone
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = null;
//这里完成对基本数据类型和String的克隆
deep = super.clone();
//对引用类型单独处理
DeepProtoType deepProtoType = (DeepProtoType) deep;
deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
return deepProtoType;
}
//深拷贝-方式2 同过对象序例化实现
public Object deepClone() {
//创建对象
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);
DeepProtoType copyObject= (DeepProtoType) ois.readObject();
return copyObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try
{
bos.close();
oos.close();
bis.close();
ois.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
5、总结:
原型模式是针对 对象复制处理