
可以把newInstance的工厂方法扩展处理,封装到框架中来减少代码的冗余
public class NotReceivingBillUtil {
private volatile static NotReceivingBillUtil helper = null;
public static NotReceivingBillUtil newInstance() {
if (helper == null) {
synchronized (NotReceivingBillUtil.class) {
if (helper == null) {
String clazzName = NotReceivingBillUtil.class.getName();
try {
Class> clazz = Class.forName(clazzName + "Ex");
helper = (NotReceivingBillUtil) clazz.newInstance();
} catch (Exception ex) {
helper = new NotReceivingBillUtil();
}
}
}
}
return helper;
}
protected void init(){
System.out.println("NotReceivingBillUtil类到初始 *** 作");
}
}
扩展类
继承原来的类对类的方法进行扩展
public class NotReceivingBillUtilEx extends NotReceivingBillUtil{
protected void init(){
System.out.println("Ex扩展类到初始 *** 作");
}
}
测试调用
public static void main(String[] args) {
NotReceivingBillUtil not =NotReceivingBillUtil.newInstance();
not.init();
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)