Consider the following: I have controllers and views in a client-application. As this runs purely on the client side, each controller must only exist once.
At first I thought about implementing everything as Singletons but this doesn't really feel right for a few reasons.
A few is instantiated like so:
abstract class View(controller: Controller)
now in each view I do certain things, based on the controller I got.
Some controllers need to have parameters set upon initialization, this speaks for a class, rather than a singleton. (Of course I could do: MyObject.set(whatever) but that's neither clean nor nice)
Now my idea was to create a simple mutable HashMap for the controllers and store the classes as keys and exactly one corresponding object as a value.
Now upon each initialization I just check the HashMap for an instance and if there is one already, I return that instead.
Seems hacky though.
Of course I could just stick with classes all the way, but I'm not sure if this is a good way. I mean, if I instantiate new Controllers every few seconds ... (It's a game we're talking about, so there is already a lot of calculations going on)