原型模式:
基于 clone 方式,实现对象的快速(简化)创建
性能保证:
- clone 是直接从内存中复制数据,不通过构造函数,所以比 new 的方式性能要好得多
- 由于不经过构造函数,所以原型模式和单例模式存在冲突。
- 用例场景:复杂对象,循环内创建等
shallow clone:仅复制基本类型及对应封装类的数据deep clone:除浅克隆的数据外,还复制数组,容器,引用对象等数据。
浅克隆外的数据,需要手动执行复制操作
实现代码:
package com.model.prototype.bean;
import java.util.Date;
public class NamedObject {
private long rrn;
private String id;
private String object;
private String objectType;
private String objectSubType;
private Date createDate;
private String createUser;
public long getRrn() {
return rrn;
}
public void setRrn(long rrn) {
this.rrn = rrn;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getObject() {
return object;
}
public void setObject(String object) {
this.object = object;
}
public String getObjectType() {
return objectType;
}
public void setObjectType(String objectType) {
this.objectType = objectType;
}
public String getObjectSubType() {
return objectSubType;
}
public void setObjectSubType(String objectSubType) {
this.objectSubType = objectSubType;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public String getCreateUser() {
return createUser;
}
public void setCreateUser(String createUser) {
this.createUser = createUser;
}
}
package com.model.prototype.bean;
public class Entity extends NamedObject {
private long parentRrn;
private String parentId;
private String operationMode;
private String location;
private String prevLocation;
private String pollutionLevel;
private String status;
public long getParentRrn() {
return parentRrn;
}
public void setParentRrn(long parentRrn) {
this.parentRrn = parentRrn;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getOperationMode() {
return operationMode;
}
public void setOperationMode(String operationMode) {
this.operationMode = operationMode;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPrevLocation() {
return prevLocation;
}
public void setPrevLocation(String prevLocation) {
this.prevLocation = prevLocation;
}
public String getPollutionLevel() {
return pollutionLevel;
}
public void setPollutionLevel(String pollutionLevel) {
this.pollutionLevel = pollutionLevel;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
package com.model.prototype.bean;
import java.util.List;
public class Process extends NamedObject {
private String disableFlag;
private String reworkInfo;
private String loopInfo;
private List versionList;
private List versionStatus;
public Process(){
}
public Process(String id){
this.setId(id);
}
public String getDisableFlag() {
return disableFlag;
}
public void setDisableFlag(String disableFlag) {
this.disableFlag = disableFlag;
}
public String getReworkInfo() {
return reworkInfo;
}
public void setReworkInfo(String reworkInfo) {
this.reworkInfo = reworkInfo;
}
public String getLoopInfo() {
return loopInfo;
}
public void setLoopInfo(String loopInfo) {
this.loopInfo = loopInfo;
}
public List getVersionList() {
return versionList;
}
public void setVersionList(List versionList) {
this.versionList = versionList;
}
public List getVersionStatus() {
return versionStatus;
}
public void setVersionStatus(List versionStatus) {
this.versionStatus = versionStatus;
}
}
package com.model.prototype.bean;
import java.util.ArrayList;
public class Product extends NamedObject {
private String owner;
private String engineerGroup;
private String qualityControlLevel;
private String icon;
private ArrayList processList;
private String material;
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getEngineerGroup() {
return engineerGroup;
}
public void setEngineerGroup(String engineerGroup) {
this.engineerGroup = engineerGroup;
}
public String getQualityControlLevel() {
return qualityControlLevel;
}
public void setQualityControlLevel(String qualityControlLevel) {
this.qualityControlLevel = qualityControlLevel;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public ArrayList getProcessList() {
return processList;
}
public void setProcessList(ArrayList processList) {
this.processList = processList;
}
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
}
package com.model.prototype;
import com.model.prototype.bean.Entity;
public class EntityPrototype extends Entity implements Cloneable {
public EntityPrototype shallowClone() {
try {
return (EntityPrototype) clone();
} catch (CloneNotSupportedException e) {
// TODO 抛出异常,应该有业务处理
}
return null;
}
}
package com.model.prototype;
import java.util.ArrayList;
import java.util.Date;
import com.model.prototype.bean.Process;
import com.model.prototype.bean.Product;
public class ProductPrototype extends Product implements Cloneable {
public ProductPrototype shallowClone() {
try {
return (ProductPrototype) this.clone();
} catch (CloneNotSupportedException e) {
// TODO 抛出异常,应该有业务处理
}
return null;
}
@SuppressWarnings("unchecked")
public ProductPrototype deepClone() {
try {
ProductPrototype pp = (ProductPrototype) this.clone();
pp.setCreateDate((Date) this.getCreateDate().clone());
pp.setProcessList((ArrayList) this.getProcessList().clone());
} catch (CloneNotSupportedException e) {
// TODO 抛出异常,应该有业务处理
}
return null;
}
}
测试:
package com.model.prototype;
import java.util.ArrayList;
import java.util.Date;
import com.model.prototype.bean.Process;
public class Test {
public static void main(String[] args) {
EntityPrototype ep = new EntityPrototype();
ep.setId("entity");
EntityPrototype ep2 = ep.shallowClone();
System.out.println(ep.getId());
System.out.println(ep2.getId());
// ============================================================
System.out.println();
ProductPrototype pp = new ProductPrototype();
pp.setId("product");
ProductPrototype pp1 = pp.shallowClone();
System.out.println(pp.getId());
System.out.println(pp1.getId());
pp.setCreateDate(new Date());
ProductPrototype pp2 = pp.shallowClone();
pp.setCreateDate(new Date(0));
System.out.println(pp.getCreateDate());
System.out.println(pp2.getCreateDate());
pp.setProcessList(new ArrayList<>());
ProductPrototype pp3 = pp.shallowClone();
Process process = new Process("process 111");
pp.getProcessList().add(process);
System.out.println(pp.getProcessList());
System.out.println(pp3.getProcessList());
System.out.println("ndeep clone");
ProductPrototype pp4 = pp.deepClone();
Process process2 = new Process("process 222");
pp.getProcessList().add(process2);
System.out.println(pp.getProcessList());
System.out.println(pp4.getProcessList());
}
}
输出结果:
entity
entityproduct
product
Thu Jan 01 08:00:00 CST 1970
Wed Jun 29 11:46:20 CST 2022
[com.model.prototype.bean.Process@7852e922]
[com.model.prototype.bean.Process@7852e922]deep clone
[com.model.prototype.bean.Process@7852e922, com.model.prototype.bean.Process@4e25154f]
Exception in thread "main" java.lang.NullPointerException
at com.model.prototype.Test.main(Test.java:45)
参考自:Java设计模式之创建型:原型模式_张维鹏的博客-CSDN博客



