原型模式
- 用原型类型指定创建对象的种类,并通过拷贝这些原型创建新的对象。
- 原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
- 代码框架如下
// 原型类
class ConcretePrototype1 extends implements Cloneable
{
private String id;
public Prototype(String id)
{
this.id=id;
}
public String getId(){
return this.id;
}
@Override
protected Object clone() throws CloneNotSupportedException{
return (Prototype)super.clone();
}
}
- 对于简历的实现
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private String timeArea;
private String company;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTimeArea() {
return timeArea;
}
public void setTimeArea(String timeArea) {
this.timeArea = timeArea;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "Resume{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
", timeArea='" + timeArea + '\'' +
", company='" + company + '\'' +
'}';
}
public Resume(String name, String sex, String age, String timeArea, String company) {
this.name = name;
this.sex = sex;
this.age = age;
this.timeArea = timeArea;
this.company = company;
}
@Override
public Object clone() throws CloneNotSupportedException{
return (Resume)super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Resume a = new Resume("大鸟","男","18","2000-2002","阿里巴巴");
//只需要调用clone方法就可以实现新简历的生成,并且可以在修改新简历的细节
Resume b = (Resume)a.clone();
b.setCompany("腾讯");
Resume c = (Resume)a.clone();
c.setCompany("百度");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
浅复制与深复制
浅复制
//原型模式
class WorkExperience{
private String timeArea;
private String company;
@Override
public String toString() {
return "WorkExperience{" +
"timeArea='" + timeArea + '\'' +
", company='" + company + '\'' +
'}';
}
public String getTimeArea() {
return timeArea;
}
public void setTimeArea(String timeArea) {
this.timeArea = timeArea;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
public WorkExperience getWorkExperience() {
return workExperience;
}
public void setWorkExperience(String timeArea, String company) {
workExperience.setTimeArea(timeArea);
workExperience.setCompany(company);
}
@Override
public String toString() {
return "Resume{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
", workExperience=" + workExperience +
'}';
}
public Resume(String name, String sex, String age) {
this.name = name;
this.sex = sex;
this.age = age;
this.workExperience = new WorkExperience();
}
@Override
public Object clone() throws CloneNotSupportedException{
return (Resume)super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Resume a = new Resume("大鸟","男","18");
a.setWorkExperience("2000-2002","阿里巴巴");
Resume b = (Resume)a.clone();
a.setWorkExperience("2000-2002","百度");
Resume c = (Resume)a.clone();
a.setWorkExperience("2000-2002","腾讯");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
- 查看上面结果,发现仅仅是对Resume对象进行了复制,而对于WorkExperience对象没有,即还是操作的同一个WorkExperience对象。
-
如果字段是值类型的,则对该字段执行逐位复制,如果字段是引用类型,则复制引用而不复制引用的对象:即原始对象及其副本引用属于同一对象。
-
这种现象叫做浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
深复制
//原型模式
class WorkExperience implements Cloneable{
private String timeArea;
private String company;
@Override
public String toString() {
return "WorkExperience{" +
"timeArea='" + timeArea + '\'' +
", company='" + company + '\'' +
'}';
}
public String getTimeArea() {
return timeArea;
}
public void setTimeArea(String timeArea) {
this.timeArea = timeArea;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public Object clone() throws CloneNotSupportedException {
return (WorkExperience)super.clone();
}
}
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
// 提供clone()方法调用的私有构造函数,克隆该对象
private Resume(WorkExperience workExperience) throws CloneNotSupportedException {
this.workExperience = (WorkExperience)workExperience.clone();
}
public WorkExperience getWorkExperience() {
return workExperience;
}
public void setWorkExperience(String timeArea, String company) {
workExperience.setTimeArea(timeArea);
workExperience.setCompany(company);
}
@Override
public String toString() {
return "Resume{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
", age='" + age + '\'' +
", workExperience=" + workExperience +
'}';
}
public Resume(String name, String sex, String age) {
this.name = name;
this.sex = sex;
this.age = age;
this.workExperience = new WorkExperience();
}
//重写clone()方法,对“WorkExperience”对象进行克隆,然后相关字段赋值
@Override
public Object clone() throws CloneNotSupportedException{
Resume resume = new Resume(this.workExperience);
resume.name = this.name;
resume.sex = this.sex;
resume.age = this.age;
return resume;
}
public static void main(String[] args) throws CloneNotSupportedException {
Resume a = new Resume("大鸟","男","18");
a.setWorkExperience("2000-2002","阿里巴巴");
Resume b = (Resume)a.clone();
a.setWorkExperience("2000-2002","百度");
Resume c = (Resume)a.clone();
a.setWorkExperience("2000-2002","腾讯");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
结果如下:深复制完成