创建类型:原型模式(Prototype): 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
从一个对象再创建另一个可指定的对象,而且不需要知道任务创建细节。
JDK中用到原型模式的有
• java.lang.Object#clone() (支持浅克隆的类必须实现java.lang.Cloneable接口)
Cloneable接口提供了一个浅克隆方法clone(),我们只要实现clone()方法就可以完成简单的原型模式的了。
具体类
/**
* Created with Intellij IDEA.
* User : Yebinghuan
* Created on 2017/8/7.
* Description :
* 简历model
* Cloneable提供了克隆方法clone
*/
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 SetPerSonalInfo(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.err.println("姓名:"+name+",性别:"+sex+",年龄"+age);
System.err.println("工作经历:"+timeArea+"月,"+company+"公司");
}
//实现对象克隆
public Object clone() {
Resume o = null;
try {
o = (Resume) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
main类
/**
* Created with Intellij IDEA.
* User : Yebinghuan
* Created on 2017/8/7.
* Description :
* 简历model
* Cloneable提供了克隆方法clone
*/
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 SetPerSonalInfo(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.err.println("姓名:"+name+",性别:"+sex+",年龄"+age);
System.err.println("工作经历:"+timeArea+"月,"+company+"公司");
}
//实现对象克隆
public Object clone() {
Resume o = null;
try {
o = (Resume) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
public class Main {
public static void main(String[] args) {
Resume a=new Resume("七月");
a.SetPerSonalInfo("男","22");
a.SetWorkExperience("9个月","厦门xxx");
//克隆一个对象
Resume b= (Resume) a.clone();
b.SetWorkExperience("6个月","厦门xx科技有限");
//再次克隆
Resume c= (Resume) a.clone();
c.SetWorkExperience("1个月","福州xx科技有限");
a.Display();
b.Display();
c.Display();
}
}
原型模式分析
public class Main {
public static void main(String[] args) {
Resume a=new Resume("七月");
a.SetPerSonalInfo("男","22");
a.SetWorkExperience("9个月","厦门xxx");
//克隆一个对象
Resume b= (Resume) a.clone();
b.SetWorkExperience("6个月","厦门xx科技有限");
//再次克隆
Resume c= (Resume) a.clone();
c.SetWorkExperience("1个月","福州xx科技有限");
a.Display();
b.Display();
c.Display();
}
}
优点
1、隐藏了对象创建的细节
2、保留了原本我们需要的对象属性
以上是原型模式的简单理解
上面的代码属于浅克隆,它无法对引用类型进行克隆。
浅克隆和深克隆
浅克隆只能对基本类型进行克隆,而深克隆可以将引用类型进行克隆。
深克隆方式的原型模式
工作经历类
/**
* Created with Intellij IDEA.
* User : Yebinghuan
* Created on 2017/8/7.
* Description :
* 工作经历
*/
public class WorkExperience implements Cloneable{
private String timeArea;
private String 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 WorkExperience clone() {
WorkExperience o = null;
try {
o = (WorkExperience) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
简历类
/**
* Created with Intellij IDEA.
* User : Yebinghuan
* Created on 2017/8/7.
* Description :
* 工作经历
*/
public class WorkExperience implements Cloneable{
private String timeArea;
private String 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 WorkExperience clone() {
WorkExperience o = null;
try {
o = (WorkExperience) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
/**
* Created with Intellij IDEA.
* User : Yebinghuan
* Created on 2017/8/7.
* Description :
* 简历model
* Cloneable提供了克隆方法clone
*/
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
public Resume(String name){
this.name=name;
workExperience=new WorkExperience();
}
//设置信息
public void SetPerSonalInfo(String sex,String age){
this.sex=sex;
this.age=age;
}
public void SetWorkExperience(String timeArea,String company){
workExperience.setTimeArea(timeArea);
workExperience.setCompany(company);
}
//显示
public void Display(){
System.err.println("姓名:"+name+",性别:"+sex+",年龄"+age);
System.err.println("工作经历:"+workExperience.getTimeArea()+"月,"+workExperience.getCompany()+"公司");
}
//实现对象克隆
public Object clone() {
Resume o = null;
try {
o = (Resume) super.clone();
o.workExperience= workExperience.clone();//对引用类型进行克隆
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
姓名:七月,性别:男,年龄22
工作经历:9个月月,厦门xxx公司
姓名:七月,性别:男,年龄22
工作经历:6个月月,厦门xx科技有限公司
姓名:七月,性别:男,年龄22
工作经历:1个月月,福州xx科技有限公司
/**
* Created with Intellij IDEA.
* User : Yebinghuan
* Created on 2017/8/7.
* Description :
* 简历model
* Cloneable提供了克隆方法clone
*/
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
public Resume(String name){
this.name=name;
workExperience=new WorkExperience();
}
//设置信息
public void SetPerSonalInfo(String sex,String age){
this.sex=sex;
this.age=age;
}
public void SetWorkExperience(String timeArea,String company){
workExperience.setTimeArea(timeArea);
workExperience.setCompany(company);
}
//显示
public void Display(){
System.err.println("姓名:"+name+",性别:"+sex+",年龄"+age);
System.err.println("工作经历:"+workExperience.getTimeArea()+"月,"+workExperience.getCompany()+"公司");
}
//实现对象克隆
public Object clone() {
Resume o = null;
try {
o = (Resume) super.clone();
o.workExperience= workExperience.clone();//对引用类型进行克隆
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
姓名:七月,性别:男,年龄22
工作经历:9个月月,厦门xxx公司
姓名:七月,性别:男,年龄22
工作经历:6个月月,厦门xx科技有限公司
姓名:七月,性别:男,年龄22
工作经历:1个月月,福州xx科技有限公司