设计模式之原型模式
目录
3.2、深拷贝实现方法一:重写clone方法来实现深拷贝(不推荐)
3.3、 深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
一、原型模式-基本介绍
基本介绍
1) 原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷 贝这些原型,创建新的对象
2) 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象, 无需知道如何创建的细节
3) 工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建 的对象通过请求原型对象拷贝它们自己来实施创建,即 对象.clone()
4) 形象的理解:孙大圣拔出猴毛, 变出其它孙大圣
思路
思路:Java中Object类是所有类的根类,Object类提供了一个clone()方法,该方法可以 将一个Java对象复制一份,但是需要实现clone的Java类必须要实现一个接口Cloneable, 该接口表示该类能够复制且具有复制的能力 => 原型模式
二、原型模式之克隆羊问题(浅拷贝)
2.1、克隆羊问题
现在有一只羊tom,姓名为: tom, 年龄为:1,颜色为:白色,请编写程序创建和tom 羊 属性完全相同的10只羊。
羊的类,且羊有个猪朋友
并重写了clone方法
public class Sheep implements Cloneable {
private String name; //名字
private int age; //年龄
private String color; //颜色
private String address = "蒙古羊"; //类型
public Pig friend; //是对象, 克隆是会如何处理
public Sheep(String name, int age, String color) {
super();
this.name = name;
this.age = age;
this.color = color;
}
@Override
public String toString() {
return "Sheep [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]";
}
//克隆该实例,使用默认的clone方法来完成
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep)super.clone();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
// TODO Auto-generated method stub
return sheep;
}
}
猪类:
public class Pig implements Cloneable{
private String name;
private int age;
public Pig(String name, int age) {
this.name = name;
this.age = age;
}
}
测试:
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("tom", 1, "白色");
sheep.friend = new Pig("jack",2);
Sheep sheep2 = (Sheep)sheep.clone(); //��¡
Sheep sheep3 = (Sheep)sheep.clone(); //��¡
Sheep sheep4 = (Sheep)sheep.clone(); //��¡
Sheep sheep5 = (Sheep)sheep.clone(); //��¡
System.out.println("sheep2 =" + sheep2 + "sheep2.friend=" + sheep2.friend.hashCode());
System.out.println("sheep3 =" + sheep3 + "sheep3.friend=" + sheep3.friend.hashCode());
System.out.println("sheep4 =" + sheep4 + "sheep4.friend=" + sheep4.friend.hashCode());
System.out.println("sheep5 =" + sheep5 + "sheep5.friend=" + sheep5.friend.hashCode());
}
}
结果分析:
所有小羊的猪朋友的hashcode相同
可见羊的frend并没有克隆,而是指向了同一个对象,这就是浅拷贝
2.2、浅拷贝
1) 对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将 该属性值复制一份给新的对象。
2) 对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类 的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内 存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个 实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成 员变量值
3) 前面我们克隆羊就是浅拷贝
4) 浅拷贝是使用默认的 clone()方法来实现 sheep = (Sheep) super.clone();
三、深入探究之深拷贝
3.1、深拷贝基本介绍
1) 复制对象的所有基本数据类型的成员变量值
2) 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变 量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对 整个对象进行拷贝
3) 深拷贝实现方式1:重写clone方法来实现深拷贝
4) 深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
3.2、深拷贝实现方法一:重写clone方法来实现深拷贝(不推荐)
猪朋友重写clone方法
public class Pig implements Cloneable{
private String name;
private int age;
public Pig(String name, int age) {
this.name = name;
this.age = age;
}
public Object clone() {
Pig pig = null;
try {
pig= (Pig) super.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return pig;
}
}
且小羊在clone方法里对于frend单独处理
package clone;
public class Sheep implements Cloneable {
private String name;
private int age;
private String color;
private String address = "蒙古羊";
public Pig friend;
public Sheep(String name, int age, String color) {
super();
this.name = name;
this.age = age;
this.color = color;
}
@Override
public String toString() {
return "Sheep [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]";
}
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep)super.clone();
//对引用数据类型单独处理
sheep.friend= (Pig) friend.clone();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
// TODO Auto-generated method stub
return sheep;
}
}
测试:
这样每个小羊的猪朋友也被克隆了
3.3、 深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
猪朋友实现
Serializable接口
import java.io.Serializable;
/**
* @author Liu
* @date 2021/2/24 - 16:00
*/
public class Pig implements Cloneable, Serializable {
private String name;
private int age;
public Pig(String name, int age) {
this.name = name;
this.age = age;
}
public Object clone() {
Pig pig = null;
try {
pig= (Pig) super.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return pig;
}
}
小羊实现Serializable接口,deepClone方法使用对象序列号实现深拷贝
import java.io.*;
public class Sheep implements Cloneable, Serializable {
private String name;
private int age;
private String color;
private String address = "蒙古羊";
public Pig friend;
public Sheep(String name, int age, String color) {
super();
this.name = name;
this.age = age;
this.color = color;
}
@Override
public String toString() {
return "Sheep [name=" + name + ", age=" + age + ", color=" + color + ", address=" + address + "]";
}
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep)super.clone();
//对引用数据类型单独处理
sheep.friend= (Pig) friend.clone();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
// TODO Auto-generated method stub
return sheep;
}
//深拷贝 - 方式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);
Sheep copyObj = (Sheep)ois.readObject();
return copyObj;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
} finally {
//关闭流
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (Exception e2) {
// TODO: handle exception
System.out.println(e2.getMessage());
}
}
}
}
测试
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("tom", 1, "白色");
sheep.friend = new Pig("jack",2);
//方式2 完成深拷贝
Sheep sheep6 = (Sheep)sheep.deepClone(); //��¡
Sheep sheep7 = (Sheep)sheep.deepClone(); //��¡
Sheep sheep8 = (Sheep)sheep.deepClone(); //��¡
Sheep sheep9 = (Sheep)sheep.deepClone(); //��¡
System.out.println("sheep2 =" + sheep6 + "sheep2.friend=" + sheep6.friend.hashCode());
System.out.println("sheep3 =" + sheep7 + "sheep3.friend=" + sheep7.friend.hashCode());
System.out.println("sheep4 =" + sheep8 + "sheep4.friend=" + sheep8.friend.hashCode());
System.out.println("sheep5 =" + sheep9 + "sheep5.friend=" + sheep9.friend.hashCode());
}
}
结果:
成功完成深拷贝
四、原型模式在spring中源码分析
配置bean.xml
<bean id="id01" class="com.atguigu.spring.bean.Monster" scope="prototype"/>
在test中获取这个对象
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("beans.xml");
//获取monster[通过id获取monster]
Object bean = applicationContext.getBean("id01");
System.out.println("bean" + bean);
在spring源码中scope="prototype"即使用了原型模式
在doGetBean中查看是否是单例的bean
五、原型模式的注意事项和细节
1) 创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提 高效率
2) 不用重新初始化对象,而是动态地获得对象运行时的状态
3) 如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化, 无需修改代码
4) 在实现深克隆的时候可能需要比较复杂的代码
5) 缺点:需要为每一个类配备一个克隆方法,这对全新的类来说不是很难,但对已有 的类进行改造时,需要修改其源代码,违背了ocp原则,这点请同学们注意.