JAVA静态方法不行么_java-公共静态方法-一个不好的迹象吗?

我刚刚在这里阅读了这篇文章:http://hamletdarcy.blogspot.com/2008/04/10-best-idea-inspections-youre-not.html,尤其是最后一点让我考虑了我的代码,特别是建议:

What in the world is a public method doing on your object that has no dependency on any fields within the object? This is certainly a code smell. The problem is that the “auto-fix” for the inspection is to apply the static keyword. Nooooo. That’s not what you want to do. A public method without any dependency on object state can’t possibly be part of an object that has one clearly stated charter. It’s just not cohesive and should be placed somewhere else. So: if the method is private, accept the auto-fix, but if the method is public then don’t.

所讨论的代码本质上是一个对象转换器.它接受类型为A的对象,并将其转换为其他类型.

我的层次结构是这样的:

接口ObjectTransformer-> GenericObjectTransformer

然后在此之下,通过ObjectTransformerA和ObjectTransformerB扩展GenericObjectTransformer

现在,ObjectTransformerA和ObjectTransformerB都需要一些功能,但实际上并不依赖GenericObjectTransformer的任何实例变量,因此它是GenericObjectTransformer中受保护的静态方法.

这是否违反上述规则?显然,这是受保护的而不是公共的,但是它仍然是可从类外部访问的方法,而该方法与类本身无关?

有什么想法吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值