7 votes

When do you use singletons?

9 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. [2]
    reese
    Link
    ECS architecture is interesting in that you have, essentially, scoped singletons. You only want one instance of a given system in a "world." A world is just a container for entities, their...

    ECS architecture is interesting in that you have, essentially, scoped singletons. You only want one instance of a given system in a "world." A world is just a container for entities, their associated components, and the systems processing them. At least, that's how many implementations model ECS, such as ECSY by Mozilla. EnTT, which is used in Minecraft, is very similar, but it uses the term "registry" instead of world.

    In practice, systems are built with specific processing in mind. They may inherit from an abstract base class provided by an ECS framework, but that's about the extent of that. Any involved object-orientation is minimal on the user's part.

    I prefer this approach to any other in the context of game dev, although some call it an anti-pattern. I can see cases for and against it just like I can for singletons more generally. I agree with the author that such patterns have respective purposes we should remember; I'm reminded of the law of instrument.

    3 votes
    1. 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.

      1 vote
  3. 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
  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?