由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个简单的:setter 和getter有什么用处?
相关主题
老年工程师转行遇到下马威为什么java要用setter和getter
子类的assignment operator 怎么访问父类的private member有没有根据 model 类自动生成 html form的工具?
C++: friend function大家都用那跟手指敲 - + = [ ] ;
c++ 设计问题求助Who can help explain setter and getter?
请教C#里property的意义scala的ide版本号已经上3冲4了
请教一个C++问题学scala发现一个有趣现象
问一个设计模式的问题golang里面 函数received type关于指针和非指针有什么难的。
goodbug vs neverlearnGetter and setter methods are evil
相关话题的讨论汇总
话题: setter话题: getter话题: 变量话题: variable话题: set
进入Programming版参与讨论
1 (共1页)
c*****m
发帖数: 1160
1

with
that
这两年经常看到一些class里面有这样的getter和setter,有什么好处?直接把这个变
量public出来不就可以了么?
相信是gof里面的一个设计模式,但是我看过几次都没有理解。
d**********x
发帖数: 4083
2
……

【在 c*****m 的大作中提到】
:
: with
: that
: 这两年经常看到一些class里面有这样的getter和setter,有什么好处?直接把这个变
: 量public出来不就可以了么?
: 相信是gof里面的一个设计模式,但是我看过几次都没有理解。

d****n
发帖数: 1637
3
class A{
var a, b,c,d,shits...
set_a(a){ this.a=a; return this;}
set_b(b){ this.b=b; return this;}
set_c(c){ this.c=c; return this;}
}
我的一点经验,怕忘member名。
书写方便,省着忘了参数名:
objA= A().set_a(a).set_b(b).set_c(c);
对比:
objA=A(a, b,c, ...);
有些参数在ctor里没用到,但是将来会用到,没必要都在开始放进去。还记不住。
z*******3
发帖数: 13709
4
spring什么等框架其实已经不用setter和getter了
但是你自己写代码
建议还是用setter/getter
因为你可以在setter/getter里面增加你自己的东西
比如get的时候用md5加密
很多时候setter/getter不仅仅是最简单的那种拷贝数值
http://pydoing.blogspot.com.au/2011/05/java-getter-and-setter.h
这个应该不是gof的东东,应该是j2ee里面常见的各种o
什么vo, dto, dao等广泛使用的东东
自己写一点j2ee分层结构,被各层的各种o搞得晕头转向的时候
就会想起这个东东来了
http://stackoverflow.com/questions/1612334/difference-between-d
g*****g
发帖数: 34805
5
This is the encapsulation principle in OOP. It looks verbose, but when you
change the implementation, you can keep the contract intact.
e.g. You may introduce another variable that's directly linked to this
variable, you are supposed to update these two values at the same time. Now
you can update that variable in the setter and all callers won't be affected.
When you are making public APIs, or modulized a big system. These patterns
to help decoupling are important.

【在 c*****m 的大作中提到】
:
: with
: that
: 这两年经常看到一些class里面有这样的getter和setter,有什么好处?直接把这个变
: 量public出来不就可以了么?
: 相信是gof里面的一个设计模式,但是我看过几次都没有理解。

d**********x
发帖数: 4083
6
Even for pure storage classes which never change it is still important to
use setter/getters for a more practical reason: debug.
if you simple use direct access, no logs can be printed and no break point
can be inserted for all of the acesses to the value -- in small projects you
can simple refactor to setter/getters, but in large project there will be
no practical way to do so.

Now
affected.

【在 g*****g 的大作中提到】
: This is the encapsulation principle in OOP. It looks verbose, but when you
: change the implementation, you can keep the contract intact.
: e.g. You may introduce another variable that's directly linked to this
: variable, you are supposed to update these two values at the same time. Now
: you can update that variable in the setter and all callers won't be affected.
: When you are making public APIs, or modulized a big system. These patterns
: to help decoupling are important.

c*********e
发帖数: 16335
7
oop原则之一,变量都是private的,不能直接动。要动,只能通过setter来改变。

【在 c*****m 的大作中提到】
:
: with
: that
: 这两年经常看到一些class里面有这样的getter和setter,有什么好处?直接把这个变
: 量public出来不就可以了么?
: 相信是gof里面的一个设计模式,但是我看过几次都没有理解。

n*w
发帖数: 3393
8
does pojo violate oop principle?

【在 c*********e 的大作中提到】
: oop原则之一,变量都是private的,不能直接动。要动,只能通过setter来改变。
c*********e
发帖数: 16335
9
pojo没违反阿,javabean里面照样有getxxx,setxxx method啊。

【在 n*w 的大作中提到】
: does pojo violate oop principle?
z*******3
发帖数: 13709
10
pojo的本意是不需要继承任何特定的接口和父类
早先年版本的ejb往往要求继承特定的接口,struts等要求继承特定的抽象类
因为这种模式其实很糟糕,群众往往弄不清楚要override的方法到底是做什么的
使得j2ee早期的推广面临着很大的困难
最常见的就是ejb的那4个接口,其中的stateful还有4个方法要override
没有多少人真能弄懂到底这些方法是做什么用的
spring出现之后,不需要继承任何特定的接口和类,简单很多很多
所以一下子就使得spring在很短时间内推广开来
全民皆编java也就是那个时候开始的
另外严格意义上的pojo连特定的annotation都不要求
但是因为annotation其实比xml要简单
所以广义的pojo就也包括那些要求使用特定annotation的java bean了

【在 c*********e 的大作中提到】
: pojo没违反阿,javabean里面照样有getxxx,setxxx method啊。
相关主题
请教一个C++问题为什么java要用setter和getter
问一个设计模式的问题有没有根据 model 类自动生成 html form的工具?
goodbug vs neverlearn大家都用那跟手指敲 - + = [ ] ;
进入Programming版参与讨论
c*****m
发帖数: 1160
11
zhaoce073 说可以在getter/setter里加入更多的process
goodbug说可以顺便管理其他变量
devilphoenix说方便debug
前两个理由是否违反“单一功能”的要求?比如md5计算,应该是另一个函数,而不是
getter了。如果顺便管理另一个函数,那么function名字就不应该是SetThisVariable(
),而应该是SetThisVariableAndAnotherVariable().
我觉得不超过 1/10的变量需要有这方面的需求;对于其他 9/10的变量就是verbose,
看起来不爽,当然也没有坏处。

you

【在 d**********x 的大作中提到】
: Even for pure storage classes which never change it is still important to
: use setter/getters for a more practical reason: debug.
: if you simple use direct access, no logs can be printed and no break point
: can be inserted for all of the acesses to the value -- in small projects you
: can simple refactor to setter/getters, but in large project there will be
: no practical way to do so.
:
: Now
: affected.

d**********x
发帖数: 4083
12
这个嘛。。我的实际感觉是比例要比1/10高很多,高多少不知道,但是想象一下,某个
关键的数据类出了问题的时候,你要监视每一个成员的变化。。。
好吧,就算是1/10,也是很高的一个比例了

【在 c*****m 的大作中提到】
: zhaoce073 说可以在getter/setter里加入更多的process
: goodbug说可以顺便管理其他变量
: devilphoenix说方便debug
: 前两个理由是否违反“单一功能”的要求?比如md5计算,应该是另一个函数,而不是
: getter了。如果顺便管理另一个函数,那么function名字就不应该是SetThisVariable(
: ),而应该是SetThisVariableAndAnotherVariable().
: 我觉得不超过 1/10的变量需要有这方面的需求;对于其他 9/10的变量就是verbose,
: 看起来不爽,当然也没有坏处。
:
: you

c*****m
发帖数: 1160
13

联结里的那个例子,用getter/setter完全没有好处啊。直接public出来不就行了?

【在 z*******3 的大作中提到】
: spring什么等框架其实已经不用setter和getter了
: 但是你自己写代码
: 建议还是用setter/getter
: 因为你可以在setter/getter里面增加你自己的东西
: 比如get的时候用md5加密
: 很多时候setter/getter不仅仅是最简单的那种拷贝数值
: http://pydoing.blogspot.com.au/2011/05/java-getter-and-setter.h
: 这个应该不是gof的东东,应该是j2ee里面常见的各种o
: 什么vo, dto, dao等广泛使用的东东
: 自己写一点j2ee分层结构,被各层的各种o搞得晕头转向的时候

c*****m
发帖数: 1160
14

:) 我把原贴更新了一下,增加了一些自己的想法。1/10是我瞎猜的

【在 d**********x 的大作中提到】
: 这个嘛。。我的实际感觉是比例要比1/10高很多,高多少不知道,但是想象一下,某个
: 关键的数据类出了问题的时候,你要监视每一个成员的变化。。。
: 好吧,就算是1/10,也是很高的一个比例了

g*****g
发帖数: 34805
15
另外一个变量可以是个private变量。比如有个变量a,为了方便或者效率,你有个另一
变量永远是2*a.
没有setter怎么弄?
设计模式不是讲究概率,目的就是为了decoupling。内部API随便写写还没事反正可以
refactor。一旦第三方要调用,你这么弄,一旦需要改,第三方都得跟着改代码。很多
时候这是不能接受的。

SetThisVariable(

【在 c*****m 的大作中提到】
: zhaoce073 说可以在getter/setter里加入更多的process
: goodbug说可以顺便管理其他变量
: devilphoenix说方便debug
: 前两个理由是否违反“单一功能”的要求?比如md5计算,应该是另一个函数,而不是
: getter了。如果顺便管理另一个函数,那么function名字就不应该是SetThisVariable(
: ),而应该是SetThisVariableAndAnotherVariable().
: 我觉得不超过 1/10的变量需要有这方面的需求;对于其他 9/10的变量就是verbose,
: 看起来不爽,当然也没有坏处。
:
: you

d**********x
发帖数: 4083
16
按照他的意思,这个东西不应该叫做setX,应该叫做updateX之类的东西

不是
verbose,

【在 g*****g 的大作中提到】
: 另外一个变量可以是个private变量。比如有个变量a,为了方便或者效率,你有个另一
: 变量永远是2*a.
: 没有setter怎么弄?
: 设计模式不是讲究概率,目的就是为了decoupling。内部API随便写写还没事反正可以
: refactor。一旦第三方要调用,你这么弄,一旦需要改,第三方都得跟着改代码。很多
: 时候这是不能接受的。
:
: SetThisVariable(

z*******3
发帖数: 13709
17
如果是public变量
任何一个方法一个线程都可以发起对该变量的修改
那么如果该调用有任何的改动
那你要搜索整个项目去寻找关于该变量的调用
然后挨个修改
但是如果你要求所有的调用全部通过方法调用
那么如果有什么改动,你只需要改动该类本身的方法就行了
举个例子
如果你要求对于某一个变量的值在get时候+1
那么如果你是public变量,那么这个+1就要你遍历项目所有的代码去修改
但是如果你用的是get,那么只需要在get方法return的时候+1就行了
如果是用方法,还有办法予以优化
因为所有调用该private变量的操作都必需经过该方法
所以可以在该方法上添加interceptor, aop之类的东东
spring也是在方法调用的时候才inject,而不是在fields被调用时候inject
直接invoke变量框架容器什么忙都帮不上
z*******3
发帖数: 13709
18
还有一个例子就是并发操作的时候
你用方法可以加synchronized关键字控制并发
如果是public,你怎么控制并发?
另外get方法也不是必然叫这个名字
可能这个名字就是update,而压根没有get方法
就只有update或者getMD5
c*****m
发帖数: 1160
19

我是这个意思,根据单一功能、明确名字的规范,getter和setter应该只处理一个变量
;如果需要增加逻辑在里面,就应该是另一个函数了。
如果我在debug过程中看到一个setter里面顺便修改了另外的值,那么我就需要跟踪所
有的getter/setter来确定是否还有类似的动作,很揪心的。
我所觉得getter/setter有用的地方就是做只读变量或者只写变量的时候。

【在 d**********x 的大作中提到】
: 按照他的意思,这个东西不应该叫做setX,应该叫做updateX之类的东西
:
: 不是
: verbose,

a9
发帖数: 21638
20
一句话:利于重构

【在 c*****m 的大作中提到】
:
: 我是这个意思,根据单一功能、明确名字的规范,getter和setter应该只处理一个变量
: ;如果需要增加逻辑在里面,就应该是另一个函数了。
: 如果我在debug过程中看到一个setter里面顺便修改了另外的值,那么我就需要跟踪所
: 有的getter/setter来确定是否还有类似的动作,很揪心的。
: 我所觉得getter/setter有用的地方就是做只读变量或者只写变量的时候。

g*****g
发帖数: 34805
21
getter setter is a java bean spec. Many frameworks rely on it to do
reflection. You have to assume the method name or you can't find it. Thus in
java, you have to call it that way. Nothing wrong to introduce extra logic
in setter to keep the object state consistent. setter is meant to change a
state of one variable. What if another variable is dependent on this
variable?

【在 c*****m 的大作中提到】
:
: 我是这个意思,根据单一功能、明确名字的规范,getter和setter应该只处理一个变量
: ;如果需要增加逻辑在里面,就应该是另一个函数了。
: 如果我在debug过程中看到一个setter里面顺便修改了另外的值,那么我就需要跟踪所
: 有的getter/setter来确定是否还有类似的动作,很揪心的。
: 我所觉得getter/setter有用的地方就是做只读变量或者只写变量的时候。

1 (共1页)
进入Programming版参与讨论
相关主题
Getter and setter methods are evil请教C#里property的意义
有人用lombok吗?请教一个C++问题
问个Python getter setter的问题问一个设计模式的问题
design patterns到底有用吗?goodbug vs neverlearn
老年工程师转行遇到下马威为什么java要用setter和getter
子类的assignment operator 怎么访问父类的private member有没有根据 model 类自动生成 html form的工具?
C++: friend function大家都用那跟手指敲 - + = [ ] ;
c++ 设计问题求助Who can help explain setter and getter?
相关话题的讨论汇总
话题: setter话题: getter话题: 变量话题: variable话题: set