this post was submitted on 12 Apr 2024
783 points (98.8% liked)

Programmer Humor

18961 readers
375 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 47 points 4 months ago* (last edited 4 months ago) (3 children)
public class AbstractBeanVisitorStrategyFactoryBuilderIteratorAdapterProviderObserverGeneratorDecorator {
    // boilerplate goes here
}
[–] [email protected] 14 points 4 months ago (1 children)

Why do you have access to my project??

[–] [email protected] 9 points 4 months ago

... And can you fix it?

[–] [email protected] 12 points 4 months ago

============ Top 5: =============== HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor: 97
AbstractAnnotationConfigDispatcherServletInitializer: 52
AbstractInterruptibleBatchPreparedStatementSetter: 49
AbstractInterceptorDrivenBeanDefinitionDecorator: 48
GenericInterfaceDrivenDependencyInjectionAspect: 47

============ Factories: ===============
DefaultListableBeanFactory$DependencyObjectFactory
ObjectFactoryCreatingFactoryBean
SimpleBeanFactoryAwareAspectInstanceFactory
SingletonBeanFactoryLocator$BeanFactoryGroup
ConnectionFactoryUtils$ResourceFactory
DefaultListableBeanFactory$DependencyProviderFactory
ObjectFactoryCreatingFactoryBean$TargetBeanObjectFactory
JndiObjectFactoryBean$JndiObjectProxyFactory
DefaultListableBeanFactory$SerializedBeanFactoryReference
AbstractEntityManagerFactoryBean$SerializedEntityManagerFactoryBeanReference
BeanFactoryAspectInstanceFactory
SingletonBeanFactoryLocator$CountingBeanFactoryReference
TransactionAwarePersistenceManagerFactoryProxy$PersistenceManagerFactoryInvocationHandler
AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler

https://gist.github.com/thom-nic/2c74ed4075569da0f80b

[–] [email protected] 2 points 4 months ago (2 children)

Okay, here we go. I'm going to spit out some bullshit and home someone corrects me if I'm wrong. I've looked for some explanations and this is what I've gotten.

Are you ready?

The Factory Pattern.

My understanding is that the purpose is a function to return any of several types of objects, but a specific type, not just an interface or whatever they might all inherit from.

I think most languages now have something like a "dynamic" keyword to solve this issue by allowing determination of the type only at runtime. (To be used with extreme caution.)

But most of the time I see the Factory pattern, it's used unnecessarily and can only return one specific type. Why they would use a Factory pattern here and not just a plain old constructor confounds me.

Am I off base?

[–] [email protected] 2 points 4 months ago

Factory pattern can return a mock type for testing or a production type, as needed, which makes it possible to unit test the code that uses the produced object.

This quick guide explains it well. Then it improves on it by explaining dependency injection.

https://github.com/google/guice/wiki/Motivation

[–] [email protected] 1 points 4 months ago

Yeah most uses of the factory pattern are unnecessary and it's mild code smell IMO. If your factory only returns one type you should definitely just use that type's constructor.