原型模式,用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。通俗的说法就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
先看一个“钱复制”的例子
1、简历类
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;
}
public void setPersonInfo(String sex,String age){
this.sex = sex;
this.age = age;
}
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);
}
@Override
protected Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
2、测试
public class Test {
public static void main(String[] args) {
Resume a = new Resume("大鸟");
a.setPersonInfo("man", "22");
a.setWorkExperience("2011-12-22", "XX公司");
Resume b = (Resume)a.clone();
b.setWorkExperience("2012-11-22", "YY公司");
Resume c = (Resume)a.clone();
c.setPersonInfo("man", "24");
a.display();
b.display();
c.display();
}
}
上面的例子是“浅复制”,让我们把工作经验用一个单独的类来表示,看看结果如何,更改简历类
3、更改后的简历类
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 void setPersonInfo(String sex,String age){
this.sex = sex;
this.age = age;
}
public void setWorkExperience(String workData,String company){
work.setWorkData(workData);
work.setCompany(company);
}
public void display(){
System.out.println(name+":"+sex+":"+age);
System.out.println("工作经历"+work.getWorkData()+":"+work.getCompany());
}
@Override
protected Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
浅复制,被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。还有一种复制是深复制,深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。下面我们来看看如何实现简历的深复制。
4、更改后的工作经验类
public class WorkExperience implements Cloneable{
private String workData;
private String company;
public String getWorkData() {
return workData;
}
public void setWorkData(String workData) {
this.workData = workData;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
protected Object clone(){
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
5、更改后的简历类
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();
}
private Resume(WorkExperience work){
this.work = (WorkExperience)work.clone();
}
public void setPersonInfo(String sex,String age){
this.sex = sex;
this.age = age;
}
public void setWorkExperience(String workData,String company){
work.setWorkData(workData);
work.setCompany(company);
}
public void display(){
System.out.println(name+":"+sex+":"+age);
System.out.println("工作经历"+work.getWorkData()+":"+work.getCompany());
}
@Override
protected Object clone(){
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.age = this.age;
obj.sex = this.sex;
return obj;
}
}