是否有一条规则表明复杂的访问方法是邪恶的?
Is there a rule that states that complex access methods are evil?
向访问方法 (getters/setters) 添加比简单地 this.x = x;
或 return x;
更多的功能通常被认为是糟糕的风格。
我正在寻找可以在技术论文中引用的书面资料。 JavaBeans specification does not contain a statement about the content of access methods. Neither does the Java Language Specification.
是否有任何正式的 Oracle 文件或具有重要意义的类似文件明确说明?还是只是不成文的规定?
编辑: 看来我错了 "usually considered bad style." 我不想开始讨论基于观点的话题。对我来说,我的回答是我不能假设它被认为是糟糕的风格。感谢您的输入!
这本来就是一种自以为是的观点,但我的印象恰恰相反。如果确实 getter/setter 仅限于简单的 return/assignment,那么直接方法将不会提供任何附加值。
setter 进行一些验证是很常见和期望的。
此外,可以为计算字段(如 getSum()、getAvg() 等)创建 getter 方法,在这种情况下,它们可能包括简单或复杂的计算。
正如@SharonBenAsher 所写的那样,这个主题非常基于观点,我支持相反的观点。
恕我直言,classes 有两种基本类型:数据传输对象(DTO / Beans)和其他。默认情况下,只有 DTO 应该有 getter(并且不太常见)setter。 "Other" class只有当有充分理由需要违反 信息隐藏 原则时,es 才应该有吸气剂。
但为什么他们不应该有(复杂的)逻辑?
我的理由是:它违反了单一责任模式。 DTO的职责是携带数据。验证数据一致性或对其进行一些计算的责任属于 uses/fills DTO 的代码。
so why have getter/setter? why not just access directly? make the DTO like a C structure – Sharon Ben Asher
由于信息隐藏原则。
仅仅因为你有一个 DTO 并不意味着你必须将值存储在一些不同的成员变量中,它可以是一个集合。
一个 DTO 也可以由其他 DTO 组成:
class Circle{
private Point center;
private double radius;
Circle(double x, double y, double r){
center = new Point(x,y);
radius=r;
}
getX(){return center.getX();}
getY(){return center.getY();}
getRadius(){return radius;}
}
直接访问属性时,您必须注意 和 您以后无法更改它...
I fail to see how a class that validates its state violates the single responsibility pattern. it may call on a utility or some external class to help in the validation process, – Sharon Ben Asher
这更糟。实用程序 class 是一个 隐藏的依赖项 ,您将其添加到下一层,将 DTO 交付到该层。只要这个 "next layer" 使用相同的 JVM,这可能不是问题,但如果它在网络连接的另一端呢?
but one cannot say it is categorically unresponsible for this task. – Sharon Ben Asher
是吗?
我自己认为我有足够的理由按照我的方式去做。如果你不同意,我也没关系。
it depends on the design and specific situation. Same with creation. Sometimes you delegate the responsibility to a factory method, sometimes you will directly call a constructor. – Sharon Ben Asher
但是如果情况发生变化(应用程序开发过程中通常会发生变化)怎么办?我将验证与数据结构分开的方法总是有效。
I think your sample code is a good example of a non-simple getter... – Sharon Ben Asher
但它没有复杂逻辑。
向访问方法 (getters/setters) 添加比简单地 this.x = x;
或 return x;
更多的功能通常被认为是糟糕的风格。
我正在寻找可以在技术论文中引用的书面资料。 JavaBeans specification does not contain a statement about the content of access methods. Neither does the Java Language Specification.
是否有任何正式的 Oracle 文件或具有重要意义的类似文件明确说明?还是只是不成文的规定?
编辑: 看来我错了 "usually considered bad style." 我不想开始讨论基于观点的话题。对我来说,我的回答是我不能假设它被认为是糟糕的风格。感谢您的输入!
这本来就是一种自以为是的观点,但我的印象恰恰相反。如果确实 getter/setter 仅限于简单的 return/assignment,那么直接方法将不会提供任何附加值。
setter 进行一些验证是很常见和期望的。
此外,可以为计算字段(如 getSum()、getAvg() 等)创建 getter 方法,在这种情况下,它们可能包括简单或复杂的计算。
正如@SharonBenAsher 所写的那样,这个主题非常基于观点,我支持相反的观点。
恕我直言,classes 有两种基本类型:数据传输对象(DTO / Beans)和其他。默认情况下,只有 DTO 应该有 getter(并且不太常见)setter。 "Other" class只有当有充分理由需要违反 信息隐藏 原则时,es 才应该有吸气剂。
但为什么他们不应该有(复杂的)逻辑?
我的理由是:它违反了单一责任模式。 DTO的职责是携带数据。验证数据一致性或对其进行一些计算的责任属于 uses/fills DTO 的代码。
so why have getter/setter? why not just access directly? make the DTO like a C structure – Sharon Ben Asher
由于信息隐藏原则。
仅仅因为你有一个 DTO 并不意味着你必须将值存储在一些不同的成员变量中,它可以是一个集合。
一个 DTO 也可以由其他 DTO 组成:
class Circle{
private Point center;
private double radius;
Circle(double x, double y, double r){
center = new Point(x,y);
radius=r;
}
getX(){return center.getX();}
getY(){return center.getY();}
getRadius(){return radius;}
}
直接访问属性时,您必须注意 和 您以后无法更改它...
I fail to see how a class that validates its state violates the single responsibility pattern. it may call on a utility or some external class to help in the validation process, – Sharon Ben Asher
这更糟。实用程序 class 是一个 隐藏的依赖项 ,您将其添加到下一层,将 DTO 交付到该层。只要这个 "next layer" 使用相同的 JVM,这可能不是问题,但如果它在网络连接的另一端呢?
but one cannot say it is categorically unresponsible for this task. – Sharon Ben Asher
是吗?
我自己认为我有足够的理由按照我的方式去做。如果你不同意,我也没关系。
it depends on the design and specific situation. Same with creation. Sometimes you delegate the responsibility to a factory method, sometimes you will directly call a constructor. – Sharon Ben Asher
但是如果情况发生变化(应用程序开发过程中通常会发生变化)怎么办?我将验证与数据结构分开的方法总是有效。
I think your sample code is a good example of a non-simple getter... – Sharon Ben Asher
但它没有复杂逻辑。