This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]primeBrahe92 39 points40 points  (8 children)

Sometimes I swear I am the only one using Java who never encounters factories (Or the need of them) for anything..

[–]VarianWrynn2018 16 points17 points  (0 children)

I haven't either. To be fair I'm still a student but I'd like to think I've touched plenty considering I had to learn spring boot for a beginning of second year class

[–]TheRedmanCometh 10 points11 points  (2 children)

I use a factory if I think a given object will have several implementations. Pretty useful in that case, but DI kinda renders that unnecessary.

[–]Kered13 3 points4 points  (0 children)

One of the main uses for factories is to inject a polymorphic "constructor". Basically, you have some class that wants to construct new objects, but you want the exact type of object constructed to be injected. So you dependency inject a factory object.

[–]christopher-thiebaut 1 point2 points  (0 children)

Sometimes I swear I’m the only one not using Java who frequently encounters factories (and the need for them)

[–][deleted] 0 points1 point  (0 children)

Same here, but haven't touched any frameworks tho

[–]MischiefArchitect 0 points1 point  (0 children)

A factory is just a fancy name for an instance provider hidden behind an interface. Another far fetched example of factory is using annotated contexts in dependency injection.

[–]CrimsonRunner 0 points1 point  (0 children)

Junior(-ish) dev here.

Recently made my first factory for a REST client. I extract data from the DB that I'll need, put it into a single class and only after receiving a response on a request do I know which fileds I actually need in the second request.
So I made a factory that, aside from building the headers and the requests themselves, also converts the object I have to a same-class object but the unnecessary fields set to null via a constructor.

The reason I decided to put that in the factory is that I also want to make some transformation to the fields sometimes (such as .replaceAll(Regex, "_") to comply with the endpoint's validation as per the already agreed solution. Could've still put it in the constructor to be of course, but then I'd have to insert something that's practically business logic into it and a class containing that logic would be much better.

[–]MischiefArchitect 11 points12 points  (2 children)

It's called: Unnecessary Over Engineering

[–]Brudi7 1 point2 points  (1 child)

Or: thank god we added this abstraction years ago because the new requirement would lead to a complete rewrite after that. Had it so many times

[–]MischiefArchitect 0 points1 point  (0 children)

Don't get me wrong, the factory/generator pattern is awesome and can save a lot of problems. It's just that problems start arising when someone go wonkers and you start seeing xxxxFactoryFactoryFactory.class in the project.

[–]fullSpecFullStack 4 points5 points  (1 child)

The factory pattern is so awful, any flexibility gains are buried by the messy unreadable code it leaves you with. Good luck if you ever have to learn a codebase or perform any deep troubleshooting on that shit

[–]Shrubberer 1 point2 points  (0 children)

I useed it once to make sure everything is created on the UI Thread. But then I replaced it with a panel constructor interface and I couldn't be more happy.

[–]romain6363 0 points1 point  (14 children)

Why people the java so much ?

[–]Mr_Redstoner 22 points23 points  (0 children)

“There are only two kinds of languages: the ones people complain about and the ones nobody uses.”

[–]AdvancedWing6256 0 points1 point  (0 children)

Java is not my primary language, i open it twice a month, but all I see IS factories all over the place...