为了测试更易读和易于编写,我通常在我的value对象中使用BuilderPattern
。例如,与其以标准方式编写此简单类,不如:
public class MyClass{
private String myProperty;
public void setMyProperty(String myProperty){
this.myProperty = myProperty;
}
}
我更喜欢这样写:
public class MyClass{
private String myProperty;
public MyClass setMyProperty(String myProperty){
this.myProperty = myProperty;
return this;
}
}
此方法会对性能产生不良影响吗?
您的代码段与使用构建器模式(GoF / Bloch)无关,而仅与使用fluent mutators
或chain setters
有关。没有实际性能影响的常见做法。
关于构建器,您还有其他构建器对象。但是在创建对象之后直接将其用于垃圾回收。
因此您可能会对内存使用产生一些影响。但是JVM确实经过了优化,可以处理此问题。