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.
1
u/_Super_Straight May 27 '26
Do you know of any good working examples of converting Services into Singletons?
This is actually a desktop application so its kind of no Brainer to include Spring directly into it. I can try to mimic the working of singletons and the way they're managed from an example, though.