Redian新闻
>
Converge of languages and design pattern
avatar
Converge of languages and design pattern# Java - 爪哇娇娃
g*y
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
avatar
s*e
2
Very good man.
"read the Gang of Four patterns book from cover to cover at least once a
year"
-Rod Johnson
avatar
c*t
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
avatar
g*y
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
avatar
p*p
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
avatar
p*p
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
avatar
c*t
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
avatar
c*t
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
avatar
m*t
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

avatar
g*g
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

avatar
c*t
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
avatar
g*y
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
avatar
c*t
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.
avatar
g*g
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-)

avatar
c*t
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

avatar
p*p
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-)

avatar
g*g
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

avatar
m*t
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.
avatar
m*t
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.
avatar
m*t
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.
:
: ,

avatar
c*t
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.

avatar
p*p
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.

avatar
g*g
23
peng!

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

avatar
m*t
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.

avatar
m*t
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

avatar
g*g
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

avatar
m*t
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.
avatar
p*p
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

avatar
g*g
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。