在项目开发中,会遇到多次实例化同一个类的情况,java中每次实例化一个对象都要执行一次构造函数,如果构造函数的执行时间很长,这个初始化操作就会影响代码运行的效率。设计模式中的原型模式就能很好地避免这种情况.
简历的复制
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
简历类
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;
public Resume(String name) {
this.name = name;
}
/**
* 设置个人信息
*
* @param sex
* @param age
*/
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
/**
* 设置工作经历
*
* @param timeArea
* @param company
*/
public void setWorkExperience(String timeArea, String company) {
this.timeArea = timeArea;
this.company = company;
}
/**
* 显示
*/
public void display() {
System.out.println(name + " " + sex + " " + age);
System.out.println("工作经历:" + timeArea + " " + 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 Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
客户端
public static void main(String[] args) throws Exception {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b = (Resume)a.clone();
b.setWorkExperience("1998-2006", "YY企业");
Resume c = (Resume)a.clone();
c.setPersonalInfo("男", "24");
a.display();
b.display();
c.display();
}
浅复制:被复制对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
工作经历类
public class WorkExperience {
private String workDate;
private String company;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
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 work;
public Resume(String name) {
this.name = name;
work = new WorkExperience();
}
/**
* 设置个人信息
*
* @param sex
* @param age
*/
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
/**
* 设置工作经历
*
* @param workDate
* @param company
*/
public void setWorkExperience(String workDate, String company) {
work.setWorkDate(workDate);
work.setCompany(company);
}
/**
* 显示
*/
public void display() {
System.out.println(name + " " + sex + " " + age);
System.out.println("工作经历:" + work.getWorkDate() + " " + work.getCompany());
}
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;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
客户端
public static void main(String[] args) throws Exception {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b = (Resume)a.clone();
b.setWorkExperience("1998-2006", "YY企业");
Resume c = (Resume)a.clone();
c.setWorkExperience("1998-2003", "ZZ企业");
a.display();
b.display();
c.display();
}
结果显示
工作经验使用了引用类型,所以克隆只是复制了引用,对引用的对象还是指向了原来的对象,所以三个引用都是最后一次设置,因为三个引用都指向了同一对象。
工作经历类
public class WorkExperience implements Cloneable {
private String workDate;
private String company;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
简历类
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private WorkExperience work;
public Resume(String name) {
this.name = name;
work = new WorkExperience();
}
public Resume(WorkExperience work) {
try {
this.work = (WorkExperience)work.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
/**
* 设置个人信息
*
* @param sex
* @param age
*/
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
/**
* 设置工作经历
*
* @param workDate
* @param company
*/
public void setWorkExperience(String workDate, String company) {
work.setWorkDate(workDate);
work.setCompany(company);
}
/**
* 显示
*/
public void display() {
System.out.println(name + " " + sex + " " + age);
System.out.println("工作经历:" + work.getWorkDate() + " " + work.getCompany());
}
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;
}
@Override
public Object clone() throws CloneNotSupportedException {
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
}
客户端
public static void main(String[] args) throws Exception {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "XX公司");
Resume b = (Resume)a.clone();
b.setWorkExperience("1998-2006", "YY企业");
Resume c = (Resume)a.clone();
c.setWorkExperience("1998-2003", "ZZ企业");
a.display();
b.display();
c.display();
}