Search Unity

Singleton vs Static

Discussion in 'Scripting' started by Cawas, Aug 23, 2013.

  1. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    Yes, I had looked at your singleton before. And now again, but maybe now with more understanding. It is basically the same singleton as mine, without the thread protection, maybe a bit easier to read, and enforcing the DontDestroy flag (my current implementation doesn't, as I mentioned). It also encourages loading on start, rather than lazy-loading, and it does one nice OnDestroy trick that I shall try to see if it prevents the problem I've mentioned in comments.

    I feel like you guys keep asking me to read your stuff and you didn't really read mine. Here, again, this is the latest update to the Toolbox idea: http://programmers.stackexchange.com/a/218322/4261

    My singleton is monobehaviour and so will be my toolbox. I don't see how that's not well suited to Unity. Your singleton is almost identical to mine, and so it sure have the same issues mine do.
     
    Last edited: Nov 13, 2013
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Oh, believe me I read your stuff. As you seem to encounter practical issues that I don't have with my implementation, I am pointing you to it.

    You mentioned some specific arguments why your singleton implementation doesn't work and I explained you that you won't have them with my one. You are trying to solve practical issues that only exist due to an implementation that you are criticizing yourself. You don't want it to create the singleton itself, but why are you having that part in the code then?
     
  3. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    Cool.

    This one instance my singleton doesn't work well is scene singleton. Basically, remove DontDestroyOnLoad from your singleton (thus making it less of a singleton) and I doubt it won't bring the same issues I had.

    If I want to have such a "scene singleton" there are ways around it. So, again, I think the toolbox is a much better way around it.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It is the first time you mentioned a specific issue with the implementation, so I took it as an example why you want to improve your singleton implementation.
    Can you name the practical weaknesses that you encountered with singletons in Unity? Or could you name the benefits of the toolbox? Yes, I have read the toolbox paper, but I don't see practical benefits. If the toolbox is better, then it should be no problem to name any practical advantage.
     
  5. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    Sorry, I've already named "a practical advantage". Search back for "scene singleton", that wasn't the first time I've mentioned this specific issue. Going beyond that is giving the dreaded example. Can't do right now.

    Wait for it. Maybe toolbox is in the limbo for so long because it's indeed useless.

    Maybe by the time I finally get the time to clean up the code, eventually start from scratch and make a better architecture, I'll find no use for the Toolbox. Because on my current project, global vars aren't avoided as much as they should be.

    But I can read the paper and see how it sounds like a much better idea than having a few singletons. Two even. I also can see many people advising in favor of service locators, and to me that's just a static toolbox.

    The point to me is, singletons are a pattern made to keep a single parameterized instance loaded until the end. And I found them also to be a good alternative to using static (thus, the title of this topic). Heck, even for static methods they're most likely better. People will argue that "global var" is not a good usage for singleton, but I never see any other option than simply using static.

    To me this is all pretty logical and evident. I agree with you. I am also missing just a practical example. I simply can't do it right now.