- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
"The result is, instead of guaranteeing that instance of an object exists (because there can be only one), and just create broad globals."
Come again?
Admin
/** * @return Returns true always. */ public boolean isSingleton() { return false; }
Returns true ... always ... ehrm, no ....
Admin
(snigger) I lied!
Admin
The only good Singleton is (was, alas) Mike.
Admin
Someone doesn't really know how IoC works, if they need a method on a object called isSingleton() lol
Admin
I hope that this is just due to someone taking old Spring code and not quite understanding it. You don't do it that way now anyway, as Spring's better at building factories and managing calling all that basic stuff than you are; often all you need to supply is a method that can actually build the object (for a class you don't fully control, like a thread pool) or mark the class for being managed that way (for your own code).
Admin
Plenty of WTF... but largely due to missing details in the article.....
Consider a factory that returns an instance by interface. I mat be a custom instance (you are the only reference as of the point of return), or it may be a shared instance [Highlander!]... Knowing this may make functional decisions later on.
As to singletons themselves, my peeve is when it is done internal to the class rather than external... Consider a Printer class with a "GetInstance) static that returns a printer... This indicates that there is only one printer in the universe. More accurately many printers may exist, but only one may be used by the application. Therefore the singleton represents THAT printer, not the printer class.
Admin
Did George run a git blame to check if the committer was Paula?
Admin
I think there's a "one" missing between "that" and "instance"
Admin
Java at its finest. Such enterprise!
Admin
I have built my share of Singletons in my days. I have also converted many of Singletons into instance classes.
Reason. The golden role of life: The only constant is change. The golden role of programming: Everything always grows
In my experience, a Singleton is just too fixed to accommodate these realities.
Admin
That actually looks very much like ThreadPoolExecutorFactoryBean has originally been written for an old version of the Spring dependency injection framework (https://spring.io/projects/spring-framework). Spring manages instance creation, initialization and caching of the result of its single call to getObject() - the factory code doesn't need to worry about anything regarding singleton pattern. Of course using such a factory outside of Spring feels totally awkward, because the boilerplate services provided by Spring are missing ...
Admin
My own view is that the Singleton pattern is in almost all cases a solution looking for a problem.
The idea of an object we only need one of is fair enough, but the idea of an object of which we can only have one is a strong red herring. (It's not completely unheard of, but as in the example of a Printer class that TheCPUWizard cites, it is often thought of as interesting / important when it isn't even correct.)
Admin
So you're saying, "If you liked it then you should have put some String on it" ?
Admin
@Steve_The_Cynic - What I do find quite useful is the idea of what is sometimes called the "Keyed Singleton Pattern, where there can be only one instance of an object type with a given (obviously unique) key value. When combined with a time/version element on the key, and a few other bells, it creates solutions that reliable scale and expand....
Admin
"Keyed Singleton Pattern"???
That's a Map !!!
Admin
Indeed, this is the "old way" of making a configurable Spring bean - and to go with this, you'd have an xml structure setting the properties and you'd just have the pool available in the application context. The isSingleton() method is also part of the interface for the BeanFactory.
Today, this seems outdated (and it is, seriously, it's been a decade!) but still works, albeit a little clunky outside of Spring.
https://www.baeldung.com/spring-beanfactory
Not a wtf, just old code.
Addendum 2022-07-06 05:23: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactory.html says
Since: 13 April 2001
So, 2 decades!
Admin
What Remy and George are missing is that it's a Spring factory class. It's not meant to be called by a developer writing Java code - the framework reads an XML descriptor, creates the class, sets the properties, and calls afterPropertiesSet to let the class instantiate itself... standard Spring behaviour (if somewhat old-school using XML these days). The getObject method is the bit that actually does something after the factory object has been fully initialised by the Spring container.
As to the isSingleton() method, the purpose is to indicate whether the factory class is guaranteed to always return the same instance on every call... which again, is part of the framework metadata tracking.
So apart from the dodgy comment, there simply isn't a WTF here... it's completely normal code from the internals of a dependency inversion framework. You'd never write code like the usage example, because the sole purpose of this code is to enable Spring to automatically supply a thread pool without interacting with the factory.
Admin
@SG -- Thank you for your second paragraph, confirming what I said earlier in my second paragraph! synergy for the win!!!
Admin
Admin
You are technically correct, and we all know that is the best kind of correct ;)
The option to not use xml for configuration of spring beans was added in 2007, in spring 2.5. https://spring.io/blog/2007/11/19/spring-framework-2-5-released I would argue that it wasn't a good substitute until spring 3.0, in 2009 - the first couple of years saw a lot of fixed bugs on the annotations.
So, my original estimate of a decade holds up :D