文章目录
- 前言
- 一、克隆羊问题
- sheep类
- clint 调用方
- 二、引入原型模式动态克隆对象
- sheep类
- clint 类
- 三、原型模式在Spring框架中源码分析
- 四、深拷贝与浅拷贝
- 完整代码
- DeepCloneableTarget
- DeepProtoType
- Client2
- 五、原型模式的注意事项和细节
前言
一、克隆羊问题
sheep类
package tanchishell.SJMS.prototype;
import java.util.Objects;
public class Sheep {
private int age;
private String name;
private String color;
public Sheep() {
}
public Sheep( String name,int age, String color) {
this.age = age;
this.name = name;
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"age=" + age +
", name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Sheep sheep = (Sheep) o;
return age == sheep.age && Objects.equals(name, sheep.name) && Objects.equals(color, sheep.color);
}
@Override
public int hashCode() {
return Objects.hash(age, name, color);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
clint 调用方
package tanchishell.SJMS.prototype;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
//传统的方法
Sheep sheep = new Sheep("tom", 1, "白色");
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());
Sheep sheep5 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
//.... System.out.println(sheep);
System.out.println(sheep2);
System.out.println(sheep3);
System.out.println(sheep4);
System.out.println(sheep5);
//... }
}
}
输出
Sheep{age=1, name='tom', color='白色'}
Sheep{age=1, name='tom', color='白色'}
Sheep{age=1, name='tom', color='白色'}
Sheep{age=1, name='tom', color='白色'}
二、引入原型模式动态克隆对象
sheep类
package tanchishell.SJMS.prototype;
import java.util.Objects;
public class Sheep implements Cloneable {
private int age;
private String name;
private String color;
private String address = "蒙古羊";
public Sheep friend; //是对象, 克隆是会如何处理, 默认是浅拷贝
public Sheep() {
}
//克隆该实例,使用默认的 clone 方法来完成
@Override
protected Object clone() {
Sheep sheep = null;
try {
sheep = (Sheep) super.clone();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return sheep;
}
public Sheep(String name, int age, String color) {
this.age = age;
this.name = name;
this.color = color;
}
@Override
public String toString() {
return "Sheep{" +
"age=" + age +
", name='" + name + '\'' +
", color='" + color + '\'' +
", address='" + address + '\'' +
", friend=" + friend +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Sheep sheep = (Sheep) o;
return age == sheep.age && Objects.equals(name, sheep.name) && Objects.equals(color, sheep.color);
}
@Override
public int hashCode() {
return Objects.hash(age, name, color);
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
clint 类
package tanchishell.SJMS.prototype;
public class Client {
public static void main(String[] args) {
System.out.println("原型模式完成对象的创建");
Sheep sheep = new Sheep("tom", 1, "白色");
sheep.friend = new Sheep("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());
System.out.println(sheep == sheep2);
}
}
输出
原型模式完成对象的创建
sheep2 =Sheep{age=1, name='tom', color='白色', address='蒙古羊', friend=Sheep{age=2, name='jack', color='黑色', address='蒙古羊', friend=null}}sheep2.friend=102206883
sheep3 =Sheep{age=1, name='tom', color='白色', address='蒙古羊', friend=Sheep{age=2, name='jack', color='黑色', address='蒙古羊', friend=null}}sheep3.friend=102206883
sheep4 =Sheep{age=1, name='tom', color='白色', address='蒙古羊', friend=Sheep{age=2, name='jack', color='黑色', address='蒙古羊', friend=null}}sheep4.friend=102206883
sheep5 =Sheep{age=1, name='tom', color='白色', address='蒙古羊', friend=Sheep{age=2, name='jack', color='黑色', address='蒙古羊', friend=null}}sheep5.friend=102206883
false
三、原型模式在Spring框架中源码分析
在使用 xml 配置 bean 时,可以选择参数 scope = ”prototype“ 来确定使用 原型模式还是单例模式,会调用到 dogetBean 方法,先判断参数是否需要使用单例模式,如果不成立会去判断 是否需要使用原型模式 else if ( mbd . isPrototype) 进行原型模式的业务逻辑
先进入 applictionContext 的 getBean 方法
进入 getBean 会先进入 assertBeanFactoryActive()方法,然后进入getBeanFactory( ),里面是同步代码块同步获取Bean工厂,从getBeanFactory( ) 方法出来后进入 getBean( ) 方法,就会尝试在工厂里面获取一个 bean 对象,genBean 会调用到 doget Bean()方法
会先判断是否为 单例模式,是不是需要创建单例对象
然后 else if 判断是否需要配置原型模式,条件成立后,会调用到 creatBean()方法返回一个原型实例
四、深拷贝与浅拷贝
从上面的输出结果我们就可以看出,克隆羊的时候,它的朋友是没有被克隆的,所以是浅拷贝,如果想要达到深拷贝的效果,可以通过实现 克隆接口和序列化接口来实现
完整代码
DeepCloneableTarget
package tanchishell.SJMS.prototype;
import java.io.Serializable;
import java.util.Objects;
public class DeepCloneableTarget implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
private String cloneName;
private String cloneClass;
//构造器
public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
//因为该类的属性,都是 String , 因此我们这里使用默认的 clone 完成即可
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
DeepProtoType
package tanchishell.SJMS.prototype;
import java.io.*;
import java.util.Objects;
public class DeepProtoType implements Serializable, Cloneable{
public String name; //String 属性
public DeepCloneableTarget deepCloneableTarget;// 引用类型
public DeepProtoType() {
super();
}
//深拷贝 - 方式 1 使用 clone 方法
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = null;
//这里完成对基本数据类型(属性)和 String 的克隆
deep = super.clone();
//对引用类型的属性,进行单独处理
DeepProtoType deepProtoType = (DeepProtoType) deep;
Object clone = deepCloneableTarget.clone();
deepProtoType.deepCloneableTarget = (DeepCloneableTarget) deepCloneableTarget.clone();
//通过强制类型转换创建了新的对象,改变了内存地址
System.out.println(deepProtoType.deepCloneableTarget.hashCode() +" ===== "+ clone.hashCode());
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 copyObj = (DeepProtoType) ois.readObject();
return copyObj;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
//关闭流
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
}
Client2
package tanchishell.SJMS.prototype;
public class Client2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DeepProtoType p = new DeepProtoType();
p.name = "宋江";
p.deepCloneableTarget = new DeepCloneableTarget("大牛", "小牛");
//方式 1 完成深拷贝
// DeepProtoType p2 = (DeepProtoType) p.clone();
// DeepProtoType p3 = (DeepProtoType) p.clone();
// DeepProtoType p4 = (DeepProtoType) p.clone();
//
// System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
// System.out.println("p2.name=" + p2.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());
// System.out.println("p3.name=" + p3.name + "p3.deepCloneableTarget=" + p3.deepCloneableTarget.hashCode());
// System.out.println("p4.name=" + p4.name + "p3.deepCloneableTarget=" + p4.deepCloneableTarget.hashCode());
//方式 2 完成深拷贝
DeepProtoType p2 = (DeepProtoType) p.deepClone();
System.out.println("p.name=" + p.name + "p.deepCloneableTarget=" + p.deepCloneableTarget.hashCode());
System.out.println("p2.name=" + p.name + "p2.deepCloneableTarget=" + p2.deepCloneableTarget.hashCode());
}
}