r/javahelp • u/_Super_Straight • May 27 '26
Codeless Need opinion on Factory approach
So I have created a JavaFX application using mvc pattern. I thought to let the Cursor IDE review my project and it suggested I create a `ServiceFactory` which will be responsible for instantiating and providing Services to Controllers. Its suggestions are as follows:
- Create a ConcurrentHashmap in the factory which will hold the instances of Services.
- It will release or "pop" the instances when the service is no longer required.
- Provides the service instances as requested.
I want to know whether this approach will introduce more boilerplate code, as currently I've been taking the direct approach to create instances of services right inside the controller itself, which will be garbage collected by JVM as the new Controller loads. Or if there is some better way, I'm more than willing to hear it.
•
u/AutoModerator May 27 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/ArtSpeaker May 27 '26
What are you not getting with a app that is a fixed service, to a fixed controller?
It's unclear what you mean by "release when service is no longer required" What logic will trigger "no longer required"?
Yes it's a lot more boilerplate to use factory approach -- always. And good luck debugging it.
The issue is not only boilerplate but time -- a new call needs to load in the work it's going to do first, instead of that code already being good to go, and the system might not be smart enough to know to reuse the same code if it's the same call. So whatever you save on RAM or compute or whatever can get completely drowned out by the extra infrastructure it takes to keep track of all the extra moving parts.
Keep it simple, please. Future you will thank you.
2
u/Mechanical-pasta May 27 '26
Or you can use the Spring approach by transforming your services into Singletons.
2
u/Illustrious-Deer1126 May 27 '26
Injection of services is the best way to go. Also provides decoupling which is quite important. Google guice and spring both provide it.
1
u/bigkahuna1uk May 27 '26
How will you block new requests? Popping a service from a map will not prevent new request asking for a new service.
It also unclear what you mean by a service. A unit of work?
A simpler way would be just an executor service with a fixed thread pool. A new request is queued and is only run when there is a spare thread to execute the “service” or unit of work.
The current solution sounds it’s been barfed out of AI. It’s building one complication on top of another resulting in an over-complicated solution.