Java构造器与构建器的使用
2024-08-24 05:29:29
我们在平常类的构建过程中,可能会面临很多问题,可扩张性、安全性等等。想象一下,这样一个场景,我们现在要创建一个类,其中有6个属性,其中又有4个属性的值是不太确定的(可能某个对象就不需要其中的某个值),这时我们怎么创建这个类呢?以下是几种方法:
使用普通构造器
public class Test {
private int servingSize;
private int servings;
private int calories;
private int fat;
private int sodium;
private int carbohydrate; public Test(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = 0;
} public Test(int servingSize, int servings, int calories) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = 0;
} public Test(int servingSize, int servings, int calories, int fat) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = 0;
} public Test(int servingSize, int servings, int calories, int fat, int sodium) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = 0;
} public Test(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
} }
我们完成该类构建后,接下来就是调用的过程:
public static void main(String[] args) {
Test test = new Test(1,2,3,0,5,6);
}
如上所示,在实例化对象时,我们需要传入相应的值,这时发现:
- 第四个参数不是我们需要的,但是还不得不给他传递一个值
- 我们在传值时,很容易出错,某两个参数值互换了位置,这在工作时是不好发现的,但是程序会报错
所以上面的方式在涉及到参数比较多,而且参数值不太确定是否需要时,这种方法会给我们的编码以及后期维护带来很大的困扰,我们再改进一下。
JavaBeans模式
public class Test {
private int servingSize;
private int servings;
private int calories;
private int fat;
private int sodium;
private int carbohydrate; public Test() { } public void setServingSize(int servingSize) {
this.servingSize = servingSize;
} public void setServings(int servings) {
this.servings = servings;
} public void setCalories(int calories) {
this.calories = calories;
} public void setFat(int fat) {
this.fat = fat;
} public void setSodium(int sodium) {
this.sodium = sodium;
} public void setCarbohydrate(int carbohydrate) {
this.carbohydrate = carbohydrate;
} }
如上,我们先创建一个无参构造方法(可以不写出来,会默认创建),接下来就是利用setter方法给属性赋值
public static void main(String[] args) {
Test test = new Test();
test.setServingSize(1);
test.setCalories(2);
test.setCalories(3);
test.setSodium(5);
test.setCarbohydrate(6);
}
如上,我们需要的对象不需要fat属性,我们就不用给其赋值,这中方法有几个好处:
- 客户端调用简单,也就是实例化对象的过程十分简单,并且不会出现把值传递出错的风险
- 别人能够很好的使用且理解简单
但是我们知道,javaBeans有个缺点:
- 线程不安全,因为对象的创建分在了好几步的过程中,不能保证对象状态的一致性
我们可以确保线程的安全,这就需要我们额外的精力(我们可以在对象构造完成,并且不允许在冻结之前使用时,手动冻结,实际中很少使用,编译器无法确定我们是否调用的freeze方法
)。因此这种方法也不够理想,还有什么继续改进的地方吗?
构建器(建造者模式的一种形式)
public class Test {
private int servingSize;
private int servings;
private int calories;
private int fat;
private int sodium;
private int carbohydrate; public static class Builder {
private int servingSize;
private int servings;
private int calories;
private int fat;
private int sodium;
private int carbohydrate; public Builder (int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
} public Builder calories(int calories) {
calories = calories;
return this;
} public Builder fat(int fat) {
fat = fat;
return this;
} public Builder sodium(int sodium) {
sodium = sodium;
return this;
} public Builder carbohydrate(int carbohydrate) {
carbohydrate = carbohydrate;
return this;
} public Test builder () {
return new Test(this);
}
} private Test(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
调用方式:
public static void main(String[] args) {
Test test = new Builder(1,2).calories(3).fat(4).sodium(5).carbohydrate(6).builder();
}
这样就保证了对象在不变的情况下,简单明了地实现了对象实例化(至于代码中的内部类,我们以后细说)。
最新文章
- jQuery中的width() innerWidth() outerWidth() outerWidth(true)的区别
- 基于jdk1.7实现的excel导出工具类
- Django--models一对多实例
- OC1_点语法
- java----线程篇
- 【转】VC的MFC中重绘函数的使用总结(整理)
- 【转】如何定制android源码的编译选项 &; 后期安装? ---- 不错
- Android4.0 Design之UI设计缺陷1
- gamma
- hadoop退出安全模式Name node is in safe mode
- Altium Desgner软件,PCB设计中铺铜的作用
- 360搜索引擎取真实地址-python代码
- PHP手册-函数参考-加密扩展
- linux命令(50):comm命令的用法,求交集
- 雷林鹏分享:Ruby 类和对象
- oracle 建表时显示ORA-00984: 列在此处不允许
- 虚拟机CentOS6.5搭建samba服务器实现文件共享
- 记一次诡异的bug调试——————关于JDK1.7和JDK1.8中HashSet的hash(key)算法的区别
- Windows下实现mysql定时备份
- 二十二 synchronized同步方法