Allen Holub 写道“你永远不应该使用 get/set 函数",他说得对吗? [英] Allen Holub wrote "You should never use get/set functions", is he correct?

查看:18
本文介绍了Allen Holub 写道“你永远不应该使用 get/set 函数",他说得对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Allen Holub 写道,

Allen Holub wrote the following,

没有耦合就没有程序.尽管如此,您可以通过严格遵循 OO(面向对象)规则(最重要的是对象的实现应该对使用它的对象完全隐藏)来大大减少耦合.例如,对象的实例变量(不是常量的成员字段)应该始终是私有的.时期.没有例外.曾经.我是认真的.(您偶尔可以有效地使用受保护的方法,但受保护的实例变量是令人厌恶的.)

You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object should be completely hidden from the objects that use it). For example, an object's instance variables (member fields that aren't constants), should always be private. Period. No exceptions. Ever. I mean it. (You can occasionally use protected methods effectively, but protected instance variables are an abomination.)

这听起来很有道理,但他接着说,

Which sounds reasonable, but he then goes on to say,

出于同样的原因,您应该永远不要使用 get/set 函数——它们只是使字段公开的过于复杂的方法(尽管访问函数返回完整的对象而不是基本的-在返回对象的类是设计中的关键抽象的情况下,类型值是合理的).

You should never use get/set functions for the same reason—they're just overly complicated ways to make a field public (though access functions that return full-blown objects rather than a basic-type value are reasonable in situations where the returned object's class is a key abstraction in the design).

坦率地说,这对我来说听起来很疯狂.

Which, frankly, just sounds insane to me.

我了解信息隐藏的原理,但是如果没有访问器和修改器,您根本无法使用 Java bean.我不知道在模型中没有访问器的情况下您将如何遵循 MVC 设计,因为模型不能负责呈现视图.

I understand the principle of information hiding, but without accessors and mutators you couldn't use Java beans at all. I don't know how you would follow a MVC design without accessors in the model, since the model can not be responsible for rendering the view.

然而,我是一个年轻的程序员,我每天都在学习更多关于面向对象设计的知识.也许有更多经验的人可以讨论这个问题.

However, I am a younger programmer and I learn more about Object Oriented Design everyday. Perhaps someone with more experience can weigh in on this issue.

推荐答案

Holub 告诉你,你通常应该避免改变对象的状态,而是求助于综合方法(行为的执行)来实现这一目标.正如 Corletk 所指出的那样,对最高抽象级别进行长期而艰苦的思考,而不是仅仅使用 getter/setter 进行编程,这只是让您围绕封装进行最终运行,这是一种智慧.

I don't have a problem with Holub telling you that you should generally avoid altering the state of an object but instead resort to integrated methods (execution of behaviors) to achieve this end. As Corletk points out, there is wisdom in thinking long and hard about the highest level of abstraction and not just programming thoughtlessly with getters/setters that just let you do an end-run around encapsulation.

然而,如果有人告诉你应该永远"不要使用 setter 或永远不要"访问原始类型,我就会遇到很多麻烦.事实上,在所有情况下保持这种纯度水平所需的努力最终会导致代码的复杂性,而不是使用适当实现的属性.您只需要有足够的意识,就可以知道何时以牺牲长期痛苦为代价来规避短期收益的规则.

However, I have a great deal of trouble with anyone who tells you that you should "never" use setters or should "never" access primitive types. Indeed, the effort required to maintain this level of purity in all cases can and will end up causing more complexity in your code than using appropriately implemented properties. You just have to have enough sense to know when you are skirting the rules for short-term gain at the expense of long-term pain.

Holub 不相信您知道其中的区别.我认为了解差异是使您成为专业人士的原因.

Holub doesn't trust you to know the difference. I think that knowing the difference is what makes you a professional.

这篇关于Allen Holub 写道“你永远不应该使用 get/set 函数",他说得对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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