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)

  • You have an unusual interpretation of Controllers and Views. If you followed the MVC pattern you wouldn't have this issue because a View is decided by the Controller and always receives the same Model definition. I'm not saying that you're wrong because I don't know your problem. However I thought it worth pointing this out in case it helps
    – Brad
    Jul 18, 2015 at 17:08
  • I'm not using MVC per se. I'm using my own MVC "light" for a game. There a controller has a view and if you click a button in a few, you call a controller method. Some of these methods create new instances of other controllers to delegate the logic there. Like for example: goToMainMenu() called in a SubMenu would activate the MainMenuController and init its view. A View here does not get any model, well, the "models" here are just Singleton-Loaders and some wrapper-classes that load/parse some data to be represented :) Now I don't want to create new Controllers each time I hit"back" and "forth
    – Sorona
    Jul 18, 2015 at 17:17

1 Answer 1


At a high level, you're essentially asking:

Is building a Controller cache better than using singletons?

I would argue that in your situation, yes, it is. And here's why.

From your description it doesn't sound like you need the semantics of a singleton here. Your game would be just fine if you instantiated a new Controller every time one was needed.

In other words, your requirement isn't: All references to a Controller must resolve to a single unique Controller state."

Instead, your requirement seems to be: When retrieving a Controller instance, it should be fast.

That is an argument for using a cache. Not singletons.

If you want to implement that cache using a HashMap then go for it :)

  • Thanks! What other options, apart from a HashMap do you see? Class as key is fine? Or would String-comparison be faster?
    – Sorona
    Jul 18, 2015 at 11:48
  • I haven't built a cache (or focused on performance for that matter) in years, so I wouldn't be able to tell you. sorry!
    – MetaFight
    Jul 18, 2015 at 11:51
  • For very small datasets, iterating an ArrayList can be faster than looking up in a HashMap. I am not sure though where the limit was. I think I read it somewhere on Stackoverflow, but I can't find the question right now.
    – Philipp
    Jul 18, 2015 at 12:25
  • @Teolha I don't know which language you are working in, but if you can get a class pointer in your language, that's likely faster than using a string as a key. Hashing a string generally has to work character by character, hashing a pointer can work on larger chunks. Nevertheless, it really depends on the hash function: a bad hash function will produce a lot of unnecessary collisions given pointers, which will slow things down. So it's the old optimization mantra: Truth is only in measurement. Jul 18, 2015 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.