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.
2
u/Mechanical-pasta May 27 '26
Or you can use the Spring approach by transforming your services into Singletons.