g*****g 发帖数: 34805 | 1 讨论一下这个问题,用一个singleton和用一个所有成员都是静态的类,
有哪些pros/cons。感觉上singleton可以实现interface,而静态类不行,
所以singleton在decouple和dyna binding上可能灵活一些。但如果我
只有一个实现呢?
比如我要做一个web service, 这个service接口很简单。类似于
Object process(int ID, Object input)
给个ID,去数据库里去取点相应数据,对input做处理然后输出。
我可以写一个bean实现这个方法, 用spring让它成为singleton来干这个活。
我也可以写一个bean,套接调用一个静态方法来干这个活。
有什么区别吗? | m******t 发帖数: 2416 | 2 I would not go with static. If you make it static, the coupling is permanent
. On the other hand, Spring keeps the wiring cost minimal, why not leverage
it?
In my OO fundamentalist opinion, Java should not have had static methods in
the first place. But then that's just me. 8-) | c*****t 发帖数: 1879 | 3 I think that We just had this discussion war a while ago :)
permanent
leverage
in
【在 m******t 的大作中提到】 : I would not go with static. If you make it static, the coupling is permanent : . On the other hand, Spring keeps the wiring cost minimal, why not leverage : it? : In my OO fundamentalist opinion, Java should not have had static methods in : the first place. But then that's just me. 8-)
| g*****g 发帖数: 34805 | 4
permanent
leverage
in
I can't agree on this last one. Class like Math are just some static methods
using a namespace. No configuration, no inheritance whatsoever. Make them
static is space efficient and faster (for static binding)
【在 m******t 的大作中提到】 : I would not go with static. If you make it static, the coupling is permanent : . On the other hand, Spring keeps the wiring cost minimal, why not leverage : it? : In my OO fundamentalist opinion, Java should not have had static methods in : the first place. But then that's just me. 8-)
| m******t 发帖数: 2416 | 5 (for coconut, too)
See, I quoted the statement with "OO fundamentalist opinion" and "that's
just me." and we are still looking at a looming flame war. 8-)
But seriously, yes, static methods are convenient. I just thought that with
so many bad programmers out there, static methods have been so abused to the
point of causing way more damage than the benefits the convenience brings.
methods
【在 g*****g 的大作中提到】 : : permanent : leverage : in : I can't agree on this last one. Class like Math are just some static methods : using a namespace. No configuration, no inheritance whatsoever. Make them : static is space efficient and faster (for static binding)
| c*****t 发帖数: 1879 | 6 我觉得因噎废食不好。什么东西都可以被 abused 。争这挺无聊的。
就比如说,为什么 SQL driver manager 用的是 static method ?
static method 很多情况下是必须的。
至于靠 object serialization 工作的 API,如果这时候争 static vs
object ,简直是没事找事。
真正懂得编程的,都不会在这种小地方浪费时间。控制好那些 API 是
要给 exposed,那些是 hidden,考虑 extensibility 和 modulization
才是王道。具体你是用什么方法实现,who cares?说难听点,也就是
底层的 programmer 争这玩意儿。
with
the
【在 m******t 的大作中提到】 : (for coconut, too) : See, I quoted the statement with "OO fundamentalist opinion" and "that's : just me." and we are still looking at a looming flame war. 8-) : But seriously, yes, static methods are convenient. I just thought that with : so many bad programmers out there, static methods have been so abused to the : point of causing way more damage than the benefits the convenience brings. : : methods
| s******e 发帖数: 493 | 7 my $0.02.
If you are going to use spring anyway in ur project, you might want to think
tow things:
1. Do you see any chances in the future that the method/class is replaced or
reimplemented for any reasons (testing, production, etc)?
2. are the calls to the class/mothod scattered in ur code?
If either of them is true, I will go for the spring way.
Otherwise if it is just a generic utility class/method used in one or two
places, static might be good enough. I believe one benefit of static method |
|