0

Here is what I'm doing:

I have a little GUI program (JavaFX) that contains a login and a main page. The main page is set up with a BorderPane. The center of the BoraderPane is filled with different sub-scenes. This depends on which content the user wants to see.

Now, some sub-scenes execute tasks which require information about the user. Hence, I need to share my user among different controllers.

I did some research and I think I found a fitting solution: Create a User-Class which implements the singleton pattern.

This is where I get stuck. Since the user is unknown I can't create the User-Class in advance. First the user has to sign-up and then I can create the Singleton.

The referred link has a UserHolder class which then holds a User object.

Question:

1 Is it possible to create a Singleton that gets injected with information (parameters) before its instance is created?

2 If this is possible, how would this be done?

Beru
  • 657
  • 1
  • 6
  • 21
  • If my memory serves me correctly, the singleton style does not work well with JavaFX. Try https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 May 12 '21 at 12:56
  • Singleton is a horrible anti-pattern. Just pass the information you need between different controllers, or use a dependency injection framework. – James_D May 12 '21 at 15:15
  • @James_D I've read the prhase "Singleton is a horrible anti-pattern" quite a lot know and I'm starting to doubt if it is even of any good. Can you clarify or lead me to some sources where this topic is discussed? I know the benefit of its design and am unclear about its drawbacks. – Beru May 12 '21 at 16:13
  • https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – James_D May 12 '21 at 16:25

0 Answers0