Attention Developers: Opportunity to contribute to Spigot / build your portfolio!

Discussion in 'Spigot Plugin Development' started by jflory7, May 8, 2015.

  1. The singleton is a valid design pattern.
  2. When a single instance (as example your plugin itself) is used over and over again within your project, a singleton is a solid choice. It helps you to shrink your methods parameters and also makes your code more readable. When someone sees "Bukkit.getBukkit()", everyone knows he gets the one and only bukkit instance. Once you pass it as "Bukkit bukkit" in your params, no one knows which bukkit it might be.


    • Readability
    • Short method params

    • Hurts the feelings of everyone that once read singletons are bad or might be less performant (not the case, see the JVM specification, static_access vs X_load)
    • Might limit extensibility of your software, when you suddenly have to run multiple instances
    The singleton is a good design pattern with many use cases in the right hands at the right time.
  3. I do see your point but in some use cases it is unreasonable to use singletons in the API. Also, they static stuff which shouldn't be static.
  4. Which is why you take a look at your plugin and see where a singleton can be used to your advantage. Also, no using the static keyword in singletons isn't wrong, making use of the static keyword to maintain one instance of your class is the entire idea of the singleton pattern.
  5. Just because singletons are a design pattern doesn't mean it has no downsides or negative effects. It's by far not a perfect design pattern. However, I do believe that for Minecraft plugins it is fairly acceptable since you won't notice anything of the static abuse from the singletons anyway. It only becomes a real problem when you're running a bunch of test cases, which realistically speaking, almost nobody does for Minecraft plugins.

    So, the singleton pattern isn't "wrong". No pattern is wrong, no pattern in perfect. It depends on your situation. For Minecraft plugins, imo singletons are fine, cuz like I said, nobody. makes test cases for Minecraft plugins. In real world applications such as micro services in Java, you'll go flat on your face with singletons (assuming you have to create test cases for everything). In such case, proper dependency injection is preferred.

    In addition the main disadvantage of a singleton pattern is not being able to write proper test cases. You can't test how a singleton behaves in different user scenarios or different states if the same instance is used across all test cases. With dependency injection using Google's Guice for example, you inject a new instance in every test. But again, we're talking about Minecraft plugins, so the main disadvantage of this pattern doesn't really apply to us since almost nobody makes test cases for Minecraft plugins because it's simply a pain. I still use the singleton pattern in some of my plugins. Some use Guice, some use this singleton pattern. Where I work however, using singleton pattern is simply unaccepted as we cannot create proper test cases & code coverage.
  6. Exactly, which is why I mentioned that: "you take a look at your plugin and see where a singleton can be used to your advantage".
    • Agree Agree x 1