源码地址:https://github.com/877148107/java-design-pattern
目录
-
基本介绍
1) 原型模式(Prototype模式)是指:用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象
2) 原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需知道如何创建的细节
3) 工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即 对象.clone()
-
Spring中的原型模式
Spring使用的原型模式主要在bean配置上面@Scope("prototype")也就是多实例的,当IOC容器创建的时候并不会去将这个实例初始化到容器,而是每次获取的时候才会调用初始化创建方法。因此每使用一次就会创建一次每次创建的实例对象是一样的,但是每次创建的实例是不相等的也就是实例对应的内存地址不一样,这就是我对原型模式的理解。
-
浅拷贝
1) 对于数据类型是基本数据类型的成员变量,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象。
2) 对于数据类型是引用数据类型的成员变量,比如说成员变量是某个数组、某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。因为实际上两个对象的该成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值
3) 浅拷贝是使用默认的 clone()方法来实现
1、案例说明
一般情况我们使用的拷贝方法都是浅拷贝。浅拷贝的类图如下,Employee拷贝一个Employee2出来,他们本身引用的Department的指向实际是同一个内存地址的对象,后面对其中一个对象的修改都会影响到另一个成员变量。
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Department department = new Department("采购部");
Employee employee = new Employee("张三",25,department);
System.out.println("拷贝前employee:"+employee+",HashCode:"+employee.getDepartment().hashCode());
Employee employee2 = (Employee) employee.clone();
System.out.println("拷贝后employee2:"+employee2+",HashCode:"+employee2.getDepartment().hashCode());
employee.getDepartment().setName("财务部");
System.out.println("拷贝赋值后employee:"+employee+",HashCode:"+employee.getDepartment().hashCode());
System.out.println("拷贝赋值后employee2:"+employee2+",HashCode:"+employee2.getDepartment().hashCode());
}
}
public class Employee implements Cloneable {
private String name;
private int age;
private Department department;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Employee(String name, int age, Department department) {
this.name = name;
this.age = age;
this.department = department;
}
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 Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", department=" + department +
'}';
}
}
public class Department {
private String name;
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department{" +
"name='" + name + '\'' +
'}';
}
}
测试结果:
拷贝前employee:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:1163157884
拷贝后employee2:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:1163157884
拷贝赋值后employee:Employee{name='张三', age=25, department=Department{name='财务部'}},HashCode:1163157884
拷贝赋值后employee2:Employee{name='张三', age=25, department=Department{name='财务部'}},HashCode:1163157884
-
深拷贝
1) 复制对象的所有基本数据类型的成员变量值
2) 为所有引用数据类型的成员变量申请存储空间,并复制每个引用数据类型成员变量所引用的对象,直到该对象可达的所有对象。也就是说,对象进行深拷贝要对整个对象进行拷贝
3) 深拷贝实现方式1:重写clone方法来实现深拷贝
4) 深拷贝实现方式2:通过对象序列化实现深拷贝(推荐)
1、案例说明
深拷贝类图如下,也就是Employee拷贝得到Employee2,那么Employee的成员变量Department对象也会拷贝一份,两个Employee都指向不同的Department对象内存地址,拷贝前后修改属性值都不会影响到另一个对象。
深拷贝方法一:使用clone方法进行深拷贝
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Department department = new Department("采购部");
Employee employee = new Employee("张三",25,department);
System.out.println("拷贝前employee:"+employee+",HashCode:"+employee.getDepartment().hashCode());
Employee employee2 = (Employee) employee.clone();
System.out.println("拷贝后employee2:"+employee2+",HashCode:"+employee2.getDepartment().hashCode());
employee.getDepartment().setName("财务部");
System.out.println("拷贝赋值后employee:"+employee+",HashCode:"+employee.getDepartment().hashCode());
System.out.println("拷贝赋值后employee2:"+employee2+",HashCode:"+employee2.getDepartment().hashCode());
}
}
public class Employee implements Cloneable {
private String name;
private int age;
private Department department;
@Override
protected Object clone() throws CloneNotSupportedException {
//这里只完成对基本数据的克隆
Employee employee = (Employee) super.clone();
//引用的数据类型单独进行克隆
employee.department = (Department) department.clone();
return employee;
}
public Employee(String name, int age, Department department) {
this.name = name;
this.age = age;
this.department = department;
}
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 Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", department=" + department +
'}';
}
}
public class Department implements Cloneable{
private String name;
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Department{" +
"name='" + name + '\'' +
'}';
}
}
测试结果:
拷贝前employee:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:1163157884
拷贝后employee2:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:1956725890
拷贝赋值后employee:Employee{name='张三', age=25, department=Department{name='财务部'}},HashCode:1163157884
拷贝赋值后employee2:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:1956725890
深拷贝方法二:
public class CloneTest {
public static void main(String[] args) throws CloneNotSupportedException {
Department department = new Department("采购部");
Employee employee = new Employee("张三",25,department);
System.out.println("拷贝前employee:"+employee+",HashCode:"+employee.getDepartment().hashCode());
Employee employee2 = (Employee) employee.deepClone();
System.out.println("拷贝后employee2:"+employee2+",HashCode:"+employee2.getDepartment().hashCode());
employee.getDepartment().setName("财务部");
System.out.println("拷贝赋值后employee:"+employee+",HashCode:"+employee.getDepartment().hashCode());
System.out.println("拷贝赋值后employee2:"+employee2+",HashCode:"+employee2.getDepartment().hashCode());
}
}
public class Employee implements Cloneable, Serializable {
private String name;
private int age;
private Department department;
/**
* 深拷贝方式一
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Object clone() throws CloneNotSupportedException {
//这里只完成对基本数据的克隆
Employee employee = (Employee) super.clone();
//引用的数据类型单独进行克隆
employee.department = (Department) department.clone();
return employee;
}
/**
* 深拷贝方式二,通过对象序列化
* @return
*/
protected 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);
Employee employee = (Employee) ois.readObject();
return employee;
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
try {
bos.close();
oos.close();
bis.close();
ois.close();
}catch (Exception e){
e.printStackTrace();
}
}
//序列化
}
public Employee(String name, int age, Department department) {
this.name = name;
this.age = age;
this.department = department;
}
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 Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", department=" + department +
'}';
}
}
public class Department implements Cloneable, Serializable {
private String name;
public Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Department{" +
"name='" + name + '\'' +
'}';
}
}
测试结果:
拷贝前employee:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:1163157884
拷贝后employee2:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:2065951873
拷贝赋值后employee:Employee{name='张三', age=25, department=Department{name='财务部'}},HashCode:1163157884
拷贝赋值后employee2:Employee{name='张三', age=25, department=Department{name='采购部'}},HashCode:2065951873
-
总结
1) 创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率
2) 不用重新初始化对象,而是动态地获得对象运行时的状态
3) 如果原始对象发生变化(增加或者减少属性),其它克隆对象的也会发生相应的变化,无需修改代码
4) 在实现深克隆的时候可能需要比较复杂的代码
5) 缺点:需要为每一个类配备一个克隆方法,这对全新的类来说不是很难,但对已有的类进行改造时,需要修改其源代码,违背了ocp原则