我们在平常类的构建过程中,可能会面临很多问题,可扩张性、安全性等等。想象一下,这样一个场景,我们现在要创建一个类,其中有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);
}

如上所示,在实例化对象时,我们需要传入相应的值,这时发现:

  1. 第四个参数不是我们需要的,但是还不得不给他传递一个值
  2. 我们在传值时,很容易出错,某两个参数值互换了位置,这在工作时是不好发现的,但是程序会报错

所以上面的方式在涉及到参数比较多,而且参数值不太确定是否需要时,这种方法会给我们的编码以及后期维护带来很大的困扰,我们再改进一下。

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属性,我们就不用给其赋值,这中方法有几个好处:

  1. 客户端调用简单,也就是实例化对象的过程十分简单,并且不会出现把值传递出错的风险
  2. 别人能够很好的使用且理解简单

但是我们知道,javaBeans有个缺点:

  1. 线程不安全,因为对象的创建分在了好几步的过程中,不能保证对象状态的一致性

我们可以确保线程的安全,这就需要我们额外的精力(我们可以在对象构造完成,并且不允许在冻结之前使用时,手动冻结,实际中很少使用,编译器无法确定我们是否调用的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();
}

这样就保证了对象在不变的情况下,简单明了地实现了对象实例化(至于代码中的内部类,我们以后细说)。

最新文章

  1. jQuery中的width() innerWidth() outerWidth() outerWidth(true)的区别
  2. 基于jdk1.7实现的excel导出工具类
  3. Django--models一对多实例
  4. OC1_点语法
  5. java----线程篇
  6. 【转】VC的MFC中重绘函数的使用总结(整理)
  7. 【转】如何定制android源码的编译选项 & 后期安装? ---- 不错
  8. Android4.0 Design之UI设计缺陷1
  9. gamma
  10. hadoop退出安全模式Name node is in safe mode
  11. Altium Desgner软件,PCB设计中铺铜的作用
  12. 360搜索引擎取真实地址-python代码
  13. PHP手册-函数参考-加密扩展
  14. linux命令(50):comm命令的用法,求交集
  15. 雷林鹏分享:Ruby 类和对象
  16. oracle 建表时显示ORA-00984: 列在此处不允许
  17. 虚拟机CentOS6.5搭建samba服务器实现文件共享
  18. 记一次诡异的bug调试——————关于JDK1.7和JDK1.8中HashSet的hash(key)算法的区别
  19. Windows下实现mysql定时备份
  20. 二十二 synchronized同步方法

热门文章

  1. const成员函数用法
  2. 2018-2019-2 网络对抗技术 20165336 Exp2 后门原理与实践
  3. keras训练cnn模型时loss为nan
  4. MySQL数据库常用命令和概念 (1)
  5. ZY
  6. Kinetis Design Studio 下使用J-Link下载程序
  7. 北京大学Cousera学习笔记--6-计算导论与C语言基础--计算机的基本原理-认识程序设计语言 如何学习
  8. gRPC学习
  9. C#保存日志文件到txt中,可追加保存,定时删除最后一次操作半年前日志文件
  10. 非常不错的svg教程