在介绍原型模式前,首先先了解下Java中的clone()方法。object中自带的本地方法
protected native Object clone() throws CloneNotSupportedException;
但是该方法不可以直接使用,需要两步操作
①实现Cloneable接口,这是一个标记接口,自身没有方法。
② 重写clone()方法
举一个小例子,看下clone()的使用方法:
编写一个Teacher类,属性name和age,Teacher类实现cloneable接口,并重写clone()方法
public class Teacher implements Cloneable {
String name;
Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
编写一个学生Stu类,属性name、age、Teacher,给每个学生指定一个老师,Stu类实现cloneable接口,并重写clone()方法
public class Stu implements Cloneable {
String name;
Integer age;
Teacher teacher;
public Stu() {
System.out.println("测试clone方法是否调用构造方法");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Stu{" +
"name='" + name + '\'' +
", age=" + age +
", teacher=" + teacher +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
编写一个测试类:
public static void main(String [] args){
Teacher t1 = new Teacher();
t1.setAge(32);
t1.setName("wang Teacher");
Stu s1 = new Stu();
s1.setName("xiao ming");
s1.setAge(22);
s1.setTeacher(t1);
System.out.println("复制前的学生1:"+s1);
try {
Stu s2 = (Stu) s1.clone();
s2.setName("da ming");
s2.setAge(24);
Teacher t2 = s2.getTeacher();
t2.setName("zhao Teacher");
t2.setAge(34);
s2.setTeacher(t2);
System.out.println("复制后的学生1:"+s1);
System.out.println("复制后的学生2:"+s2);
System.out.println("teacher的地址"+s1.getTeacher().hashCode());
System.out.println("teacher的地址"+s2.getTeacher().hashCode());
} catch (CloneNotSupportedException e) {
}
}
实验结果:
Connected to the target VM, address: '127.0.0.1:52313', transport: 'socket'
测试clone方法是否调用构造方法
复制前的学生1:Stu{name='xiao ming', age=22, teacher=Teacher{name='wang Teacher', age='32'}}
复制后的学生1:Stu{name='xiao ming', age=22, teacher=Teacher{name='zhao Teacher', age='34'}}
复制后的学生2:Stu{name='da ming', age=24, teacher=Teacher{name='zhao Teacher', age='34'}}
teacher的地址1763847188
teacher的地址1763847188
Disconnected from the target VM, address: '127.0.0.1:52313', transport: 'socket'
Process finished with exit code 0
显然,在给学生da ming指定老师名字和年龄时,把xiao ming学生的老师也修改了,这就是clone()方法的浅拷贝造成的。复制xiao ming学生的name和age时,成功复制了对应的存储空间到daming中,其实,teacher的存储空间已对应的复制了,只是复制的是相同的引用,看结果中的两个老师的地址都是1763847188,这两个地址指的是同一存储空间,所以,在修改一个老师的同时也就把另一名老师修改了。解决办法就是深拷贝,重写学生类中的clone()方法,
/**
* 调用clone()方法时,student中的name和age复制了空间,teacher也复制了空间,但空间中存的引用值是不会变的,还指向的是原来的引用
*/
@Override
protected Object clone() throws CloneNotSupportedException {
Stu newstudent = (Stu) super.clone();
newstudent.teacher = (Teacher) teacher.clone();
return newstudent;
}
实验结果:
测试clone方法是否调用构造方法
复制前的学生1:Student{name='xiao ming', age='22', teacher=Teacher{name='wang Teacher', age='32'}}
复制后的学生1:Student{name='xiao ming', age='22', teacher=Teacher{name='wang Teacher', age='32'}}
复制后的学生2:Student{name='da ming', age='24', teacher=Teacher{name='zhao Teacher', age='34'}}
teacher的地址1836019240
teacher的地址325040804
Process finished with exit code 0
完成~
=========================分割线==================
原型模式为设计模式其中之一,也是重新实现的clone()方法
原型模式可以通过一个对象实例确定创建对象的种类,并且通过拷贝创建新的对象。原型模式实际上就是从一个对象创建另一个新的对象,使新的对象有具有原对象的特征。
编写一个个人individual类和工作经历work类,并直接复制成一个新的对象:
work类:
public class Work {
String address;
String company;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "work{" +
"address='" + address + '\'' +
", company='" + company + '\'' +
'}';
}
}
individual类
public class Individual implements Cloneable{
String name;
Integer age;
Work work;
public Individual() {
work = new Work();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
/* public Work getWork() {
return work;
}
public void setWork(Work work) {
this.work = work;
}*/
public void setWork(String address,String company) {
work.address = address;
work.company = company;
}
public void setperson(String name,Integer age){
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Individual{" +
"name='" + name + '\'' +
", age=" + age +
", work=" + work +
'}';
}
@Override
protected Individual clone() throws CloneNotSupportedException {
Individual individual = new Individual();
individual.setperson(this.name,this.age);
individual.setWork(this.work.address,this.work.company);
return individual;
}
}
测试类
public class PrototypeDemo {
/**
* 实现原型模式 实质就是实现clone()方法的深拷贝
* 1.实现cloneable()接口,并重写object的clone()方法
* 2. 在clone()方法中实现拷贝,并返回一个拷贝实体。
*/
public static void main(String [] args) throws CloneNotSupportedException {
Individual individual1 = new Individual();
individual1.setperson("xiaoming",22);
individual1.setWork("beijing","baidu");
System.out.println("个人信息拷贝前individual1"+individual1);
Individual individual2 = individual1.clone();
individual2.setperson("daming",24);
individual2.setWork("hangzhou","ali");
System.out.println("个人信息拷贝后individual1"+individual1);
System.out.println("个人信息拷贝后individual2"+individual2);
}
}```
实验结果
个人信息拷贝前individual1Individual{name='xiaoming', age=22, work=work{address='beijing', company='baidu'}}
个人信息拷贝后individual1Individual{name='xiaoming', age=22, work=work{address='beijing', company='baidu'}}
个人信息拷贝后individual2Individual{name='daming', age=24, work=work{address='hangzhou', company='ali'}}