A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by Tad Dicks:
You could also look at the double checked singleton:
Originally posted by Tad Dicks:
You could also look at the double checked singleton:
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
Beware though - before Java 5, double checked locking didn't work reliably.
Originally posted by Ernest Friedman-Hill:
There are three choices that work. The simplest is the initialized static member version. The second is the synchronized getInstance() method. The third one is to use an extra class, a static member class, which hold nothing but the initialized static member; the getInstance() method can then reference it. The static member class will only be loaded, and the singleton instantiated, when getInstance() is called.
Originally posted by Ken Blair:
It still doesn't if the variable isn't volatile. The costs associated with volatile variables in the new memory model means you still don't get a performance gain out of it either.
"I'm not back." - Bill Harding, Twister
Note that Head First Design Patterns does specifically say that the pattern only works if the variable is volatile, and only in JDK 5 or later.
Tony Morris
Java Q&A (FAQ, Trivia)
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by ak pillai:
For a global singleton, you need to use an independent service like a RMI service, which can be shared between applications.
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
For a global singleton, you need to prove that the universe is finite and then assert that there exists only "one" in (finite) spacetime.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Tony Morris:
For a global singleton, you need to prove that the universe is finite and then assert that there exists only "one" in (finite) spacetime. What you have described is not a singleton, but merely "application scoped data". All data is scoped within some finite boundary (be it a local declaration or higher), therefore, either all data is a singleton or no data is a singleton. I am opting for the latter definition.
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by Tony Morris:
For a global singleton, you need to prove that the universe is finite and then assert that there exists only "one" in (finite) spacetime. What you have described is not a singleton, but merely "application scoped data". All data is scoped within some finite boundary (be it a local declaration or higher), therefore, either all data is a singleton or no data is a singleton. I am opting for the latter definition.
if i use this singleton in webapplication it wont work.(coz tomcat and weblogic servers uses their own classloaders )
hence i can prove that singleton is an anti pattern it's not a design pattern
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Ken Blair:
Or your definition of Singleton differs from other posters. Isn't it possible the rest of us consider a class for which there can only exist a single instance per class loader a "Singleton"?
Tony Morris
Java Q&A (FAQ, Trivia)
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Tony Morris:
Sure it's possible.
I have a few questions then:
1) Under this definition, every static member is a "singleton" - you must agree or shift the definition
2) Every "class" is a singleton - again, agree or shift
3) Aren't "design patterns" supposedly "language agnostic"? Isn't it then a contradiction within its very own definition? Shift the definition of design pattern or singleton - as it stands, they are orthogonal. Simply, a "singleton" is not a design pattern, and every static member is a singleton - why not just call it a static member?
4) Why "class loader"? Why some arbitrary scope? "Because the book said so" is my best explanation so far - which I simply don't buy. How about "because the establishment said so"? Again, history shows that established beliefs are - if anything - generally false. Therefore, it is not sound evidence. Or simply, "because everyone else said so" is void. Any other explanations are welcome.
I blame the illegitimate authoritarian texts that claim to purport truth and naive subscribers willingly bow down - this side issue has resulted in my declaration of being a sworn enemy to what I perceive as nonsense and exploitation of those who have no choice but to trust the word of others. The exploitation may be intentional or not, but is irrelevant to the issue at hand.
Unfortunately (for myself more often than not), I am an optimist, hence my continual rant.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ken Blair:
1. A static member is not a class.
2. There exist many instances of Class.
3. I'm not experienced or knowledgeable enough to dare make sweeping statements about "design patterns" or OOP, so whether or not a design pattern is by definition "language agnostic" I'll have to defer on. That being said it is my impression that when referring to design patterns we're talking about solutions to common problems in object oriented software design. As far as I'm aware there is no accepted formal definition for Singleton that implies it must be one instance per universe, solar system, planet, continent, application, era, year, machine, language, etc. In other words, when talking about a Singleton we're talking about a class that is designed such that there can only be one instance. In what context this is depends on the discussion.
4. I would think it's because a Singleton in the context of "per ClassLoader" is the most commonly used and has the behavior most commonly expected. Admittedly, I can't back this up, it's just an opinion based entirely on experience which is limited. If I break out my GoF book (which you may not consider an authorative source) and I take a look at the intent, motivation, applicability, structure, participants, collaboration and consequences I think the "per ClassLoader" definition and solutions most closely resemble the pattern and the most common uses in Java applications.
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by Tony Morris:
1. A static member is class-loader scoped data, therefore, it fits your definition of "singleton" and so is a singleton.
Originally posted by Tony Morris:
2. A Class is class-loader scoped data, therefore, it is a "singleton".
Originally posted by Tony Morris:
3. I'll shorten the research for you, singleton is not a "design pattern" - in fact, no such thing (design pattern) really exists - one need only start looking at the mathematical foundations of requirements analysis to deduce this somewhat simple and obvious conclusion. Like a singleton, one can continually shift definitions recursively (it is recursive - just watch someone try to say that points 1 and 2 are wrong by shifting definition) in an attempt to save some notion that one may (unfortunately) be attached to in some way - even if some party were to hold conclusive proof before the subject.
Originally posted by Tony Morris:
4. Fine, I now call all class-loader scoped data "singleton". This will be fun By the way, what happens when there are 2 class loaders? Oh wait, the books don't ever do that. Here is what I once thought (until now in order to be socially compliant) was a singleton:
Originally posted by Tony Morris:
The local declaration 'singleton' is a singleton - only one exists in the scope given by a pair of braces. One may define a new pair of braces with a different "singleton", but they are still both a singleton.
Here is another singleton:
The field declaration singleton is a singleton - only one exists in the scope of an instance of Y. One may define a new instance of Y, but they are still both singletons.
Here is another one:
The static field declaration singleton is a singleton - only one exists in the scope of Z.class and only one Z.class exists within the scope of a class loader. This singleton also fits my new (ok, I am being facetious) definition of singleton, since the books (or is it "everyone else"?) said so.
Originally posted by Tony Morris:
I offer you an experiment - a thought experiment. Put yourself in a cage for 50 years. In those 50 years, you are placed under duress and forced to write software - call it say, WebSphere. In that software, you are responsible for one specific part of it - one that involves using different class loaders several times a day. You quickly learn that something that exists in one class loader may also exist in another class loader (but in a different point in spacetime). After 50 years you exit the cage, and you denounce any notion of a "singleton being class loader scoped" data, however, you find that "everyone else" - who rarely does what you do - believes otherwise.
The question now is, is it simply the "community standard" that defines a singleton (pulled from books, etc.)? If not, then is the definition of the aforementioned caged subject illegitimate? Or can both definitions exist independently? For example, I might have a notion of a singleton, today, and I might have a different definition tomorrow as might you (anyone). All definitions are legitimate and a "singleton" therefore exists "in one's head".
Just which is it?
Originally posted by Tony Morris:
The question now is, is it simply the "community standard" that defines a singleton (pulled from books, etc.)?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
You consistently say that there is no such thing as a Singleton, however the examples you give and the arguments you make seem to indicate to me that you simply aren't working off of the same definition.
Tony Morris
Java Q&A (FAQ, Trivia)
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Originally posted by saikrishna cinux:
oh,man you are now making a relationship with physical sciences
PLEASE ,dont compare java, and computer fundamentals with physics,maths or any other scrap
Tony Morris
Java Q&A (FAQ, Trivia)
Originally posted by saikrishna cinux:
or any other scrap
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
In the first class, instantiation of the singleton instance is done inside the getInstance() method. This method is not synchronized. Hence, two different threads may access it simultaneously. In such a scenario, it is possible that the null-check and instantiation are done by both the threads. This will result in two instances of the class (or for that matter, theoritically, n instances for n number of threads).
Originally posted by Ilja Preuss:
Watch your language, please. We should be able to disagree with each other without becoming abusive. Keep in mind that misunderstandings are prevalent in written communication, and that we therefore have to be more careful here than in face to face conversations.
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|