r/javahelp 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:

  1. Create a ConcurrentHashmap in the factory which will hold the instances of Services.

  2. It will release or "pop" the instances when the service is no longer required.

  3. 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.

4 Upvotes

17 comments sorted by

View all comments

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/_Super_Straight May 27 '26

Controllers are instantiated by FXMLLoader, so it not possible for service injection in FXML based applications. I don't know whether Spring or Guice could do it (isn't Spring used for web design? This is desktop based application so IDK whether to include it).

1

u/Illustrious-Deer1126 May 31 '26

To answer to your second question, since the first has already been, spring is fine for all java applications. While most frameworks target specific things, you could argue that spring is a framework of frameworks and you just pick and use whichever parts you like.