由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Converge of languages and design pattern
相关主题
static getInstance()array 在java里 是一定放在heap 吗?
thread safe Singleton 的几种方法?basic java question
看到一个关于singleton的面试题question: how to implement this
SingletonSingleton vs. static class?
一个关于generics的问题Will Singleton Pattern work in Clustered Env?
三论abstract class要准备java面试, 看什么书比较好?
关于singletonJava EE 习题 1
今天下午要面一个老印Where does Java Store Static Variable?
相关话题的讨论汇总
话题: singleton话题: instance话题: function话题: static话题: ruby
进入Java版参与讨论
1 (共1页)
g**********y
发帖数: 14569
1
After exploring AJAX for a couple of weeks, I have some observations:
* Someone said: Smalltalk never die, it just be absorbed by other languages,
gradually. Look at the evolving of programming language, it's exactly like
human language, new words are coming every day, but only good ones stay;
others become cliche and outdated. In Ruby, I see Java, I see Perl. In AJAX,
I see Ruby. On the other way, Java starts to absorb ideas from Ruby.
Obviously: good design/idea spread around.
* Bad idea sprea
s******e
发帖数: 493
2
Very good man.
"read the Gang of Four patterns book from cover to cover at least once a
year"
-Rod Johnson
c*****t
发帖数: 1879
3
Actually, I am a big fan of static / singleton codes :)
Basically, I think of static functions as utility functions.
It is easy to wrap a static function inside a instance function. Never
forget that an instance function is only meaningful either when it
involves an instance variable or when one intends to override it. Yet,
since one usually prefer composition over inheritance, so creating an
instance function only when instance variables are involved. Besides
static function is much faster t
g**********y
发帖数: 14569
4
Singleton is not evil as a pattern. But it is so easily to get singletonis
for a programmer.
For every complex problem, there is always an answer that is clear, simple,
and wrong. -- H. L. Mencken
Singleton is such an answer in many cases.
1. In real life, most programmers use singleton as global data holder/
processor. Think this way -- if your 30 classes all use/depend on a class,
isn't that too tight dependency? Is that evil, yes. Why? You find it is so
pain to change in future, because 30 cl
p***p
发帖数: 559
5
One example:
I am working on a Servlet Container, so there are paar of Factorys, which
are obtained from a singletone Abstract factory.
I can use a singleton class to encapseln all factorys and can be accessed
easily anywhere. Or set the factories in the "root" Server class all other
components have to obtained reference to Server first.
I chosed the later way, because it shows the relations between all
components.
The pattern I have used for this Servlet Container
Basic Patterns: interface, get
p***p
发帖数: 559
6
again my question:
I am working on a Servlet Container, so there are paar of Factorys, which
are obtained from a singletone Abstract factory.
I can use a singleton class to encapseln all factorys and can be accessed
easily anywhere. Or set the factories in the "root" Server class all other
components have to obtained reference to Server first.
I chosed the later way, because it shows the relations between all
components.

constraint
ask
c*****t
发帖数: 1879
7
Actually, I am a big fan of static / singleton codes :)
Basically, I think of static functions as utility functions.
It is easy to wrap a static function inside a instance function. Never
forget that an instance function is only meaningful either when it
involves an instance variable or when one intends to override it. Yet,
since one usually prefer composition over inheritance, so creating an
instance function only when instance variables are involved. Besides
static function is much faster t
c*****t
发帖数: 1879
8
Actually, I am a big fan of static / singleton codes :)
Basically, I think of static functions as utility functions.
It is easy to wrap a static function inside a instance function. Never
forget that an instance function is only meaningful either when it
involves an instance variable or when one intends to override it. Yet,
since one usually prefer composition over inheritance, so creating an
instance function only when instance variables are involved. Besides
static function is much faster t
m******t
发帖数: 2416
9
Short answer: Use Spring.
Long answer: Use any DI container.
8-)

【在 p***p 的大作中提到】
: again my question:
: I am working on a Servlet Container, so there are paar of Factorys, which
: are obtained from a singletone Abstract factory.
: I can use a singleton class to encapseln all factorys and can be accessed
: easily anywhere. Or set the factories in the "root" Server class all other
: components have to obtained reference to Server first.
: I chosed the later way, because it shows the relations between all
: components.
:
: constraint

g*****g
发帖数: 34805
10

This is not Singleton, in Singleton, one and only one instance is created.

【在 p***p 的大作中提到】
: One example:
: I am working on a Servlet Container, so there are paar of Factorys, which
: are obtained from a singletone Abstract factory.
: I can use a singleton class to encapseln all factorys and can be accessed
: easily anywhere. Or set the factories in the "root" Server class all other
: components have to obtained reference to Server first.
: I chosed the later way, because it shows the relations between all
: components.
: The pattern I have used for this Servlet Container
: Basic Patterns: interface, get

相关主题
三论abstract classarray 在java里 是一定放在heap 吗?
关于singletonbasic java question
今天下午要面一个老印question: how to implement this
进入Java版参与讨论
c*****t
发帖数: 1879
11
Actually, I am a big fan of static / singleton codes :)
Basically, I think of static functions as utility functions.
It is easy to wrap a static function inside a instance function. Never
forget that an instance function is only meaningful either when it
involves an instance variable or when one intends to override it. Yet,
since one usually prefer composition over inheritance, so creating an
instance function only when instance variables are involved. Besides
static function is much faster t
g**********y
发帖数: 14569
12
Keep your eyes open :) as I know, many serious Java/OO experts start to
notice the impact of Ruby or already involved in Ruby development. A good
example is Martin Fowler and his thoughtworks colleagues, I find a lot of
them working in some Ruby open source.
Tim Bray (XML spec writer) publish a blog to complain about Ruby. On the
same day, a college student in Australia rebuted him. And that open a web-
war. On same day, I saw so many blogs are discussing this incident.
Java will not become Ruby
c*****t
发帖数: 1879
13
We are arguing for nothing :)
A: C is evil because it can be evil in the wrong hand.
B: C is good because it can be good in the right hand.
A: There must be gun control.
B: Guns don't kill people; people kill people.
g*****g
发帖数: 34805
14
I don't think so, you can't just get rid of Singletons.
If you are using some service which you can't/won't take care of
the construction, factory is the way to go, if you only need one instance,
which is the case for most programming against interface, you need
singleton, both can be alleviated by DI though.

,

【在 m******t 的大作中提到】
: Short answer: Use Spring.
: Long answer: Use any DI container.
: 8-)

c*****t
发帖数: 1879
15
I like singleton :) I use it alot to avoid create multiple identical
instances and saves the trouble tracking instances.
As for design pattern, iterator is implemented by Sun developer so
others don't have to worry about it. Conventional devlopers rarely
need to develop Collection sub-classes and thus making it appear that
iterator is built into the language, but it is not. Design pattern
is not slow. A pattern is pattern because people code the similar
thing and come to conclusion to use th

【在 g**********y 的大作中提到】
: After exploring AJAX for a couple of weeks, I have some observations:
: * Someone said: Smalltalk never die, it just be absorbed by other languages,
: gradually. Look at the evolving of programming language, it's exactly like
: human language, new words are coming every day, but only good ones stay;
: others become cliche and outdated. In Ruby, I see Java, I see Perl. In AJAX,
: I see Ruby. On the other way, Java starts to absorb ideas from Ruby.
: Obviously: good design/idea spread around.
: * Bad idea sprea

p***p
发帖数: 559
16
again my question:
I am working on a Servlet Container, so there are paar of Factorys, which
are obtained from a singletone Abstract factory.
I can use a singleton class to encapseln all factorys and can be accessed
easily anywhere. Or set the factories in the "root" Server class all other
components have to obtained reference to Server first.
I chosed the later way, because it shows the relations between all
components.

constraint
ask

【在 m******t 的大作中提到】
: Short answer: Use Spring.
: Long answer: Use any DI container.
: 8-)

g*****g
发帖数: 34805
17

This is not Singleton, in Singleton, one and only one instance is created.

【在 p***p 的大作中提到】
: One example:
: I am working on a Servlet Container, so there are paar of Factorys, which
: are obtained from a singletone Abstract factory.
: I can use a singleton class to encapseln all factorys and can be accessed
: easily anywhere. Or set the factories in the "root" Server class all other
: components have to obtained reference to Server first.
: I chosed the later way, because it shows the relations between all
: components.
: The pattern I have used for this Servlet Container
: Basic Patterns: interface, get

m******t
发帖数: 2416
18
Singletons are evil. Period.
If you think Foo.getInstance().doSomething() is simple and straightforward,
come back after a few years and 46 different consultants have worked on it
and the code that calls it.
m******t
发帖数: 2416
19
Singletons are evil. Period.
If you think Foo.getInstance().doSomething() is simple and straightforward,
come back after a few years and 46 different consultants have worked on it
and the code that calls it.
m******t
发帖数: 2416
20

That's because you are fortunate enough to work on a project that either
only hires programmers who know what they are doing, or has a very stringent
process. Trust me, there are way more projects that do not operate like
that than those that do. 8-)
That's true, but singleton works more like this "it takes a good, well
disciplined, and careful programmer to _not_ shoot his or somebody else's
foot with the gun called 'singleton'". So, yes, in that case, we are going
to blame the tools. 8-)

【在 g*****g 的大作中提到】
: I don't think so, you can't just get rid of Singletons.
: If you are using some service which you can't/won't take care of
: the construction, factory is the way to go, if you only need one instance,
: which is the case for most programming against interface, you need
: singleton, both can be alleviated by DI though.
:
: ,

相关主题
Singleton vs. static class?Java EE 习题 1
Will Singleton Pattern work in Clustered Env?Where does Java Store Static Variable?
要准备java面试, 看什么书比较好?web app 每天更新数据一次
进入Java版参与讨论
c*****t
发帖数: 1879
21
What's your point? List your reason please.
Of course, no one should abuse any patterns, but that does not mean a
particular pattern is bad/evil.

,

【在 m******t 的大作中提到】
: Singletons are evil. Period.
: If you think Foo.getInstance().doSomething() is simple and straightforward,
: come back after a few years and 46 different consultants have worked on it
: and the code that calls it.

p***p
发帖数: 559
22
Thanx, i mean i am building a Servlet Container, so it it basicly J2SE.
There is tons of docs about spring on line, can you introduce me a relative
short and useful guide?

【在 g*****g 的大作中提到】
:
: This is not Singleton, in Singleton, one and only one instance is created.

g*****g
发帖数: 34805
23
peng!

【在 m******t 的大作中提到】
: Short answer: Use Spring.
: Long answer: Use any DI container.
: 8-)

m******t
发帖数: 2416
24

You really need to see the difference between my point and that above in
order to "get it".
My point is "C is evil because it's very hard to stay good with it." 8-)
I'm not saying "we shouldn't have guns". I'm saying "let's lock them up so
people can't shoot people so easily." 8-)

【在 c*****t 的大作中提到】
: We are arguing for nothing :)
: A: C is evil because it can be evil in the wrong hand.
: B: C is good because it can be good in the right hand.
: A: There must be gun control.
: B: Guns don't kill people; people kill people.

m******t
发帖数: 2416
25
Short answer: Use Spring.
Long answer: Use any DI container.
8-)

【在 p***p 的大作中提到】
: again my question:
: I am working on a Servlet Container, so there are paar of Factorys, which
: are obtained from a singletone Abstract factory.
: I can use a singleton class to encapseln all factorys and can be accessed
: easily anywhere. Or set the factories in the "root" Server class all other
: components have to obtained reference to Server first.
: I chosed the later way, because it shows the relations between all
: components.
:
: constraint

g*****g
发帖数: 34805
26
Spring IoC is your friend, dependency is inevitable, it's better to keep
it in a centralized configurable place. And you save writing some
boilerplate code.

【在 p***p 的大作中提到】
: again my question:
: I am working on a Servlet Container, so there are paar of Factorys, which
: are obtained from a singletone Abstract factory.
: I can use a singleton class to encapseln all factorys and can be accessed
: easily anywhere. Or set the factories in the "root" Server class all other
: components have to obtained reference to Server first.
: I chosed the later way, because it shows the relations between all
: components.
:
: constraint

m******t
发帖数: 2416
27
I'm sure you guys all read theserverside, but just in case you missed this
piece:
http://www.theserverside.com/blogs/thread.tss?thread_id=42116
Despite that the author got pretty much every one of his statements about
Spring wrong, he did give a rather comprehensive list of what's-wrong-with-
singletons in the blog.
p***p
发帖数: 559
28
I do not like singleton, especially for beginner. I told a guy here in my
team something about singleton. After that his code looks like written in
BASIC, full of singleton and static ...
I think the handoff of references through interface is very important, you
are forced to get clear the relation between all classes, nobody falls from
sky.
I would only use singelton for some kind of factory, configure or helper
class.
Tow more questions:
1. How can I detory a singleton class.
2. Even I am work

【在 c*****t 的大作中提到】
: I like singleton :) I use it alot to avoid create multiple identical
: instances and saves the trouble tracking instances.
: As for design pattern, iterator is implemented by Sun developer so
: others don't have to worry about it. Conventional devlopers rarely
: need to develop Collection sub-classes and thus making it appear that
: iterator is built into the language, but it is not. Design pattern
: is not slow. A pattern is pattern because people code the similar
: thing and come to conclusion to use th

g*****g
发帖数: 34805
29
I don't think most java programmers use singletons as global data holder,
not in the project I am involved. Singletons are pretty much used where
one and only one instance, often coupled with factory to achieve layer
separation. And IMHO, fix what is wrong, a bad programmer can always
shoot his foot in an unimaginable way, don't blame the tools.

,

【在 g**********y 的大作中提到】
: Singleton is not evil as a pattern. But it is so easily to get singletonis
: for a programmer.
: For every complex problem, there is always an answer that is clear, simple,
: and wrong. -- H. L. Mencken
: Singleton is such an answer in many cases.
: 1. In real life, most programmers use singleton as global data holder/
: processor. Think this way -- if your 30 classes all use/depend on a class,
: isn't that too tight dependency? Is that evil, yes. Why? You find it is so
: pain to change in future, because 30 cl

1 (共1页)
进入Java版参与讨论
相关主题
Where does Java Store Static Variable?一个关于generics的问题
web app 每天更新数据一次三论abstract class
guice, spring和ejb关于singleton
话说有一个模式叫做Multiton今天下午要面一个老印
static getInstance()array 在java里 是一定放在heap 吗?
thread safe Singleton 的几种方法?basic java question
看到一个关于singleton的面试题question: how to implement this
SingletonSingleton vs. static class?
相关话题的讨论汇总
话题: singleton话题: instance话题: function话题: static话题: ruby