创建对象lombok生成器和手动声明的更快方法 [英] faster way to create object lombok builder vs manually declaration

查看:185
本文介绍了创建对象lombok生成器和手动声明的更快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这三种创建对象的方式之间,哪种执行速度更快?

Between these 3 way to create an object, which one is the faster in time of execution ?

Amount.builder().categoryCode("A").coveredAmount(new 
BigDecimal(100)).build();

Amount cva1 = new Amount();
cva1.setCoveredAmount(new BigDecimal(100));
cva1.setCategoryCode("A");

Amount cva1 = new Amount(new Bigdecimal(100), "A");

字段数可以有所不同吗?

And the number of fields can be made difference ?

推荐答案

除了性能比较(花生btw ...)之外,您不应该根据对象的执行时间使用对象创建策略,而应该仅根据您的实际需要和要求使用对象创建策略和最佳做法.

Beyond the performance comparison (peanuts btw...) you should not use a object-creation strategy based on his execution time but merely on your real need , requirements and best practices.

  1. Fluent Builder pattern

  • 更好的可读性
  • 可选属性
  • 分步施工

Remark:不要使用此模式来掩盖您的类定义了太多字段的事实……使用类似extract class的模式,而不要进行构建器构造.

Remark : don't use this pattern to hide the fact that your class defines too many fields... use pattern like extract class instead of going through a builder construction.

  1. Javabean style.这种类型的构造(IMO)是一种不好的做法,因为从无参数构造到调用setter对其进行初始化之间的时间,对象仍然处于不一致状态.

  1. Javabean style. This type of construction is (IMO) a bad practice because the time between the no-arg construction and calls to setters to initialize it, the object stills in an inconsistent state.

Primary constructor.当所有字段均为required时,您更喜欢这种方式.

Primary constructor. You prefer this way when all fields are required.

这篇关于创建对象lombok生成器和手动声明的更快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆