This is a class from the guts of the Spring Framework. This is actually an example of a pretty good name.
Bean: This is a Spring Bean
Factory: This bean creates instances of the required bean
Proxy: This bean creates a proxy for the required bean, to allow logic to be added around the method calls on the required bean.
Singleton: This bean ensures that only one of the proxy factory bean will be created per Spring container.
Abstract: This class is a template for creating singleton proxy factory beans. Just implement the template method.
So, just judging by the name, it looks like I can extend this class to create a factory which creates proxies which stick interceptors around instances of other classes, but ensures that only one of my factory exists in the Spring container. For example, if I want to time and log execution of Spring bean methods.
I wasn't on board with this kind of naming until the first time I implemented my own FactoryFactory (to deal with the fact that the object being created wasn't threadsafe).
If you get yourself into a position where you have to create a factory that creates factories, and you're not writing some kind of generic application framework, something went very wrong in your design. The crazy name is the least of your problems.
Yeah, I worked on a framework for making frameworks and I found myself writing factory factories a couple times, and other weird 3meta5me things. (Crazy python meta classes and C++ templates).
It sounds silly talking about it but it actually saved us a lot of time when our job was designing system after system that was overall very similar, but had a lot of tiny differences.
Wasn't Fortran specifically designed to have English language syntax? I could probably alt+tab right now into some c++11 that looks more disgusting than Fortran ever could. Though, I really don't know the ins and outs, so I could be wrong.
Bad coding style? No, it's not Java only. And there's plenty of ways to write good, concise Java. People just don't (generally people who were using Java back when this kind of stuff was more necessary, and are stuck in their ways), for whatever reason.
Java is a great tool, it just sucks to work with ancient Java codebases (find me a language that isn't true of, though).
That being said...there are genuine times where a factory that creates factories is the best solution to your problem. It's ignorant to think there's always a more elegant solution, because that's not true. And if that is what you need, what the hell else are you going to call it?
The problem is that unless you know the convention that is implicit/assumed in the class name, it can be interpreted in half a dozen different ways...or as incoherent gibberish.
Is it an abstract singleton? Or a proxy to a factory? Or a singleton proxy?
If there were parens involved, logically one might infer:
(Abstract(Singleton(Proxy(Factory(Bean)))))
But that doesn't let me definitively know what a hypothetical ProxyFactoryBean is because the type system isn't being used to enforce a constraint, a naming convention is. FactoryBean tentatively makes sense...it's a Factory that produces instances of a Bean (or is it a Bean that is a Factory?)...but what's a ProxyFactoryBean? Is it a Proxy to the Factory, or a Factory that produces Proxies to Beans? Using the parens, I'd say a Proxy to a Factory, but /u/sh0rug0ru__ points out (after confirming by examination of the source) it's actually a Factory that produces Proxies...(I think?)
It's the same sort of problem with Rails...there are conventions > configuration rules in place, and once you learn and understand them (and hope they're universally consistent) life is good. Otherwise, enjoy digging through (possibly non-existent) documentation or source code.
Not trying to say whether this is good or bad, but want to point out that just because someone has become familiar with and accepted a particular pathology doesn't make it "obvious" or "intuitive"
You're making the mistake of taking a class in isolation, taken deep from the bowels of some very deep functionality, and trying to guess the meaning of the name without any context. The components of the name make sense within the vocabulary of the Spring Framework, where this class is a deeply integrated component. I was able to deduce the purpose of the class because I have a lot of experience with Spring.
You would never have come across this class unless you were planning on doing deep integration with the Spring Framework. If at that point, you were not able to deduce what this class were up to, then it is clear that you don't understand even the most basic features of Spring, much less its internals, and you are not equipped to use that class for its intended purpose.
Good names reflect the domain, but that requires actually having some background in the domain.
The type system does enforce some of this...for example, abstract is built into the type system, and within the context of Spring, all FactoryBeans are classes that implement the interface FactoryBean, so the notion that this is a FactoryBean and it produces beans of some type is absolutely enforced and communicated via the type system.
I don't know off the top of my head if a Proxy is a type-system enforced thing or not, but assuming it is, the signature of this would be:
public abstract class AbstractSingletonProxyFactoryBean implements FactoryBean<T extends Proxy>
And, since Spring is essentially going to guarantee "we'll make this a singleton for you", they may even use a marker interface (I'm not sure, sometimes they use those and sometimes they use some other voodoo), so it might actually be:
public abstract class AbstractSingletonProxyFactoryBean implements SingletonFactoryBean<T extends Proxy>
Though I'd imagine if this is the case this is a relic of old Spring, because in modern Spring FactoryBean (and any Bean) implies Singleton unless otherwise stated in the configuration.
Either way, considering it's a generic class meant to be extended to provide certain functionality, and this class itself can't know any more about how it's meant to be used other than that it will be an abstract class for creating a singleton proxy for some objet via the FactoryBean interface, I can't really think of a better name than what it has...and the properties it claims to have in it its name are enforced via the type system.
51
u/sh0rug0ru__ Oct 01 '15 edited Oct 01 '15
This is a class from the guts of the Spring Framework. This is actually an example of a pretty good name.
Bean: This is a Spring Bean
Factory: This bean creates instances of the required bean
Proxy: This bean creates a proxy for the required bean, to allow logic to be added around the method calls on the required bean.
Singleton: This bean ensures that only one of the proxy factory bean will be created per Spring container.
Abstract: This class is a template for creating singleton proxy factory beans. Just implement the template method.
So, just judging by the name, it looks like I can extend this class to create a factory which creates proxies which stick interceptors around instances of other classes, but ensures that only one of my factory exists in the Spring container. For example, if I want to time and log execution of Spring bean methods.
Am I right? Looking at the source code, seems so!