由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Singleton vs. static class?
相关主题
一个关于generics的问题求教:Facade Pattern vs Static Variable
basic java questionStatic就是个混球
static getInstance()请教一个Queue实现的问题
thread safe Singleton 的几种方法?请教一个动态cast的问题
Where does Java Store Static Variable?如何造Array of Generic Type
折腾了一天,实在是绝望了,请教请教Converge of languages and design pattern
增加点难度 java corenew in JSP 2.0 spec
问个primitive type的问题what's inside an java object?
相关话题的讨论汇总
话题: static话题: singleton话题: class话题: methods话题: object
进入Java版参与讨论
1 (共1页)
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
1 (共1页)
进入Java版参与讨论
相关主题
what's inside an java object?Where does Java Store Static Variable?
How to know the size of a java object ?折腾了一天,实在是绝望了,请教请教
RMI里面,server和client是decoupled的么?增加点难度 java core
今天笔试时候有一两个地方没答上来,有些不爽问个primitive type的问题
一个关于generics的问题求教:Facade Pattern vs Static Variable
basic java questionStatic就是个混球
static getInstance()请教一个Queue实现的问题
thread safe Singleton 的几种方法?请教一个动态cast的问题
相关话题的讨论汇总
话题: static话题: singleton话题: class话题: methods话题: object