7 votes

When do you use singletons?

11 comments

  1. Omnicrola
    Link
    My first development job referenced the GoF a lot. It's a great reference, however like all tools they can be misused. There was a long running Java project that had the Singleton pattern used in...

    My first development job referenced the GoF a lot. It's a great reference, however like all tools they can be misused. There was a long running Java project that had the Singleton pattern used in it's early construction, and later developers myself included (there was very high turnover) saw it and had a tendency to use it again for what where (in retrospect) not justifiable reasons.

    If I could go back to that project I would probably argue against it's use. I'd argue against a lot of architectural decisions on that project knowing what I know now....

    3 votes
  2. [4]
    Comment deleted by author
    Link
    1. [3]
      ChuckS
      Link Parent
      ECS is the new hotness for Unity; I was wondering if you've used it and what your opinion was.

      I prefer this approach to any other in the context of game dev, although some call it an anti-pattern.

      ECS is the new hotness for Unity; I was wondering if you've used it and what your opinion was.

      2 votes
      1. [3]
        Comment deleted by author
        Link Parent
        1. ChuckS
          Link Parent
          No it's great, thanks for taking the time to post. I've been using Unity for industrial automation simulations. I'd like to get on board with DOTS and ECS, but the documentation and official...

          No it's great, thanks for taking the time to post. I've been using Unity for industrial automation simulations. I'd like to get on board with DOTS and ECS, but the documentation and official examples are just so damn hard to find. I was brought up on OOP, but I'm a mechanical engineer, so I'm competent at the basics but not especially advanced in the finer points of programming techniques (though I'm sincerely trying... Reading the GoF book now).

          It's like a lot of advanced techniques to be - I can read the code and think, "Oh yeah, I understand how that works," but I find I'm having a hard time imagining for myself where or how I would convert my OOP class to ECS/DOTS.

          Not helping anything for me is that I'm not sure sometimes what I should be using, or what the exact difference is, between systems and jobs.

          Do I make a job to get a speed boost or do I make an entity, add components, and use a system to get a speed boost? I simulate laser scanners, so I make raycasts in the 6 figures per second. I use batched raycasts on one scanner object, but would it be better to have one scanner with 5 or 10 thousand laser beam entities?

          I can't find good tutorials (sorry) on the topic, so it's hard to find reference projects that show situations similar to my projects that have a clear discussion of how to transition to ECS/DOTS, or why*, or whether you should use jobs or entities.

          *When I say no discussion of why, I understand that there's a performance boost. I think ultimately the thing I'm looking for, that I would have hoped Unity would have put out, what an explanation of the design pattern that would drive someone to use DOTS instead of OOP. Something like, "if you find that your class looks like X, or if you find you're constantly doing Y, then you should consider migrating that functionality to ECS by <doing the things>."

          Again, I'm a somewhat experienced programmer, I'm studying design patterns, but the patterns I am learning are all based on the OOP paradigm. I don't know where to find a good reference on DOTS design patterns.

          2 votes
        2. ChuckS
          Link Parent
          Also the Amethyst Engine looks neat. As I've been diving more into programming I've been looking for open source projects I might be able to contribute to. I haven't tried Rust, but it keeps...

          Also the Amethyst Engine looks neat. As I've been diving more into programming I've been looking for open source projects I might be able to contribute to. I haven't tried Rust, but it keeps scoring high on the Stack Overflow list of loved languages, so I'm interested in giving it a shot.

          I'm not a multibody simulation professional, but I have used the Featherstone solver package that the man himself wrote for Matlab and could probably port his stuff to any other language. You said they have a transform hierarchy already so I don't know that Amethyst would need/want the Featherstone stuff, but I sincerely believe it's going to be the most significant release with the Unity 2020.1 update so if they're not interested now then they might be shortly (or longly lol; I've been waiting months for 2020.1 to officially release.)

          1 vote
  3. [2]
    skybrian
    Link
    One thing singletons allow over globals is gradual migration to not using singletons. If you have a singleton of type Foo, then you can make functions that take a Foo parameter, so the caller can...

    One thing singletons allow over globals is gradual migration to not using singletons. If you have a singleton of type Foo, then you can make functions that take a Foo parameter, so the caller can select an alternate Foo at runtime. Meanwhile the rest of the system uses the singleton.

    As an example of a reasonable use of singletons, Go's http package has "DefaultClient" and "DefaultServeMux" global variables, but is designed so you could also write your code to work with independent http.Client and http.ServeMux values.

    They make sense when there is usually a 1:1 mapping between the singleton object and the process, but sometimes there isn't.

    A downside is that it can be difficult to enforce the constraint that a particular library should not use the singleton. Custom linter rules can help, but the singleton could end up being used indirectly through another library.

    Another problem is that if the singleton turns out to be a bad default and it's used pervasively, you're stuck. An example of this is the default character encoding in Java, which is inherited from the operating system. It's common, particularly in servers, to standardize on UTF8, but Java code often ends up using the default encoding by accident if you're not careful.

    2 votes
    1. [2]
      Comment deleted by author
      Link Parent
      1. skybrian
        Link Parent
        Yes, it's a global and in the simple case there is no difference, but typically, rather than being a simple global like an integer or string, a singleton is a group of related globals. If you have...

        Yes, it's a global and in the simple case there is no difference, but typically, rather than being a simple global like an integer or string, a singleton is a group of related globals. If you have your globals grouped into a structure then it's easier to pass the whole group around, compared to if they were all top-level. Compare with a namespace or package, which are other ways of grouping globals together that aren't as easily converted into a struct.

  4. KonstantineBeridze
    Link
    It's an interesting article about singlton patterns. What do you guys think about singletons?

    It's an interesting article about singlton patterns. What do you guys think about singletons?

    1 vote
  5. [2]
    The-Toon
    Link
    I haven't read design patterns, so I only have a passing exposure to them. I've used a singleton once, to provide the representation of null in the host language when implementing an interpreter....

    I haven't read design patterns, so I only have a passing exposure to them. I've used a singleton once, to provide the representation of null in the host language when implementing an interpreter. In the language I'm implementing (child language?) the null value is a singleton, so I made the value a singleton in the host language.

    1 vote
    1. mose
      Link Parent
      That's interesting. I don't have experience with implementing languages, but that seems like a common (?) approach -- true, false, and nil are singletons in Ruby, I believe.

      That's interesting. I don't have experience with implementing languages, but that seems like a common (?) approach -- true, false, and nil are singletons in Ruby, I believe.

      2 votes
  6. viridian
    Link
    In Java I used them all the time for a way of owning information that needed to always exist and was always atomic. Now that my work has switched over to React based JS, I don't think I've seen a...

    In Java I used them all the time for a way of owning information that needed to always exist and was always atomic. Now that my work has switched over to React based JS, I don't think I've seen a single singleton, which isn't much of a surprise given how react handles state. The interoperability between state's mutable history for things like breadcrumbs, and singletons, is likely right around zero.

    1 vote
  7. ohyran
    Link
    Ok so NO ONE else read "When do you use Simpletons?" and thought it was some kind of way of tricking idiots in to doing your laundry, painting a fence or something?

    Ok so NO ONE else read "When do you use Simpletons?" and thought it was some kind of way of tricking idiots in to doing your laundry, painting a fence or something?