1.使用场景:构造函数逻辑复杂,new对象成本高,提高性能。
如果一个类的构造方法为空,那么new这个类的对象成本很低,比clone()方法的成本低得多,但是如果这个类的构造方法稍微复杂一些(比如有两个参数赋值),那么clone()方法回避new这个类的对象性能高得多。
下面举个场景例子:
例如风控进件,有非常多的参数,但是进件对象需要经过多重规则引擎修改其中的变量值,但是我们还希望保留原始的进件参数,所以使用原型模式去复制对象,使用复制后的对象与规则引擎交互。
class RiskInputParam implements Cloneable{
/**
* 订单金额
*/
private BigDecimal orderPrices;
/**
* 用户累计实物订单数
*/
private Integer physicalOrdersNums;
/**
* 下单IP
*/
private String ip;
/**
* 福卡激活时间
*/
private Date fukaEnableTime;
/**
* 订单主单号
*/
private String orderSn;
/**
* 订单主单号
*/
private String mobile;
/**
* 下单手机号
*/
private String orderMobile;
/**
* 订单类型:0普通实物 2手机充值 3卡券 4京东订单 5直冲 6美团 7集采订单 8春秋航空 9蛋糕叔叔 10高德打车 11裕福会员
*/
private Integer orderType;
/**
* 订单主单号
*/
private String rechargeMobile;
/**
* 订单主单号
*/
private Integer differentMobileRechargeNum;
/**
* 手机号是否为虚拟号
*/
private Integer virtualMobile;
private int riskLevel;
public RiskInputParam(BigDecimal orderPrices, Integer physicalOrdersNums, String ip, Date fukaEnableTime, String orderSn, String mobile, String orderMobile, Integer orderType, String rechargeMobile, Integer differentMobileRechargeNum, Integer virtualMobile) {
this.orderPrices = orderPrices;
this.physicalOrdersNums = physicalOrdersNums;
this.ip = ip;
this.fukaEnableTime = fukaEnableTime;
this.orderSn = orderSn;
this.mobile = mobile;
this.orderMobile = orderMobile;
this.orderType = orderType;
this.rechargeMobile = rechargeMobile;
this.differentMobileRechargeNum = differentMobileRechargeNum;
this.virtualMobile = virtualMobile;
}
public int getRiskLevel() {
return riskLevel;
}
public void setRiskLevel(int riskLevel) {
this.riskLevel = riskLevel;
}
public RiskInputParam clone() throws CloneNotSupportedException{
return (RiskInputParam) super.clone();
}
}
class RuleEngine{
public static RiskInputParam rule1(RiskInputParam riskInputParam){
riskInputParam.setRiskLevel(1);
return riskInputParam;
}
public static RiskInputParam rule2(RiskInputParam riskInputParam){
riskInputParam.setRiskLevel(2);
return riskInputParam;
}
}
public class PrototypeMode {
public static void main(String[] args) throws Exception{
RiskInputParam riskInputParam = new RiskInputParam(new BigDecimal("20"),1,"111.111.111.111",new Date(),"S54325325235235","99999999","43434343434",1,"8888888888",1,1);
long time1 = System.nanoTime();
RiskInputParam riskInputParam2 = new RiskInputParam(new BigDecimal("20"),1,"111.111.111.111",new Date(),"S54325325235235","99999999","43434343434",1,"8888888888",1,1);
RiskInputParam riskInputParam3 = new RiskInputParam(new BigDecimal("20"),1,"111.111.111.111",new Date(),"S54325325235235","99999999","43434343434",1,"8888888888",1,1);
RiskInputParam riskInputParam4 = new RiskInputParam(new BigDecimal("20"),1,"111.111.111.111",new Date(),"S54325325235235","99999999","43434343434",1,"8888888888",1,1);
RiskInputParam riskInputParam5 = new RiskInputParam(new BigDecimal("20"),1,"111.111.111.111",new Date(),"S54325325235235","99999999","43434343434",1,"8888888888",1,1);
long time2 = System.nanoTime();
RuleEngine.rule1(riskInputParam);
RuleEngine.rule2(riskInputParam2);
System.out.println("初始化所需时间:"+(time2-time1));
long time4 = System.nanoTime();
RiskInputParam clone = riskInputParam.clone();
RiskInputParam clone2 = riskInputParam.clone();
RiskInputParam clone3 = riskInputParam.clone();
RiskInputParam clone4 = riskInputParam.clone();
RiskInputParam clone5 = riskInputParam.clone();
long time5 = System.nanoTime();
RuleEngine.rule1(riskInputParam);
RuleEngine.rule2(clone);
System.out.println("使用原型模式初始化所需时间:"+(time5-time4));
}
}
初始化所需时间:16795
使用原型模式初始化所需时间:12596
这里通过构造函数创建了5个对象,与调用clone()浅克隆出5个对象,每次都是浅克隆用时少。