Using Main class as singleton

Discussion in 'Plugin Development' started by _Filip, Jul 14, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    _Filip

    Why is having the main class be a singleton bad programming practise.

    Things like a static Main.getMain() method.
    I don't do this, and I likely won't, as I don't like doing it. I am just curious as to why people say this is bad.

    As an example, the NMS MinecraftServer.getServer() is a method that retrieves the singleton MinecraftServer object.
     
  2. Offline

    Jogy34

    I'm not completely sure. I've never actually hear that making your main class a singleton is bad practice. Outside of bukkit I've never actually had a use for my main class in anything though as I usually just use it to start some other class (eg. starting a game that runs in a different class). With bukkit though, I do sometimes create a 'getMain()' method in my main class for some of my plugins because I don't want to have to worry about passing in an instance of it to things that only ever need it once or twice.

    Singletons are typically used when you don't want multiple objects of a type. Using your example of the getServer() method, you wouldn't want two server objects running at the same time on the same server as that could easily mess up a lot of things so it only allows one MinecraftServer object to be created (with a few workarounds if you really wanted to create another MinecraftServer object for whatever reason).
     
  3. I have no idea if this has anything to do with this, but I create a Main.getMain() method so I can access the plugin easier. For example in schedulers. Other than that I think they just aren't that useful... (in bukkit).
     
  4. Offline

    _Filip

    Jogy34
    Of course, but you also don't want more than one Main class instance too :p
     
  5. Offline

    Gater12

    Jogy34
    Could use JavaPlugin.getPlugin(Class c) to get corresponding plugin instance based of your Main class.
     
  6. Offline

    Jogy34

    Or you could save an instance of your main class to more easily, more safely, and more efficiently retrieve it.
     
  7. Offline

    Gater12

    Jogy34
    What's wrong with JavaPlugin.getPlugin(Main.class) ? Pretty sure it's more efficient then storing your whole Main class instance to a static variable.
     
  8. Offline

    ResultStatic

    Gater12 i used that one and i got weird errors sometimes on reloads, from now on i use a static instance
     
  9. Offline

    Gater12

    ResultStatic
    It's usually how you use it that causes problems.
     
  10. Offline

    Rocoty

    You mean storing a reference to the object? No that wouldn't take much space and would be more efficient. The reason we "hate" static around here is so newbs shouldn't go using it without knowing why, because that is dangerous business. How you get your main instance usually doesn't matter, be it passing through constructors, using JavaPlugin::getPlugin(Class), or the half-arsed static field way.

    All are equally valid when it comes to performance and resource use (which doesn't really matter in this case). When it comes to style, I'd say the constructor way is both simpler and what some would call "more correct" in terms of OOP.
     
    Zupsub and Gater12 like this.
  11. Offline

    1Rogue


    All of this, plus the fact that once you start using a singleton pattern it becomes much more difficult to maintain other classes as not being singleton. Or you end up with all your classes using "MyPlugin.getInstance()./*etc*/", and results in extremely coupled code.
     
    TheSpherret, Traks and Rocoty like this.
  12. Offline

    Jogy34

    The variable just stores a reference to the object which already exists so it wouldn't be stopping the gc from cleaning up the object or anything like that. The getPlugin(clazz) method goes through a few checks to make sure that the class that you pass in is actually a plugin class and throws exception if it isn't. Compared to retrieving a reference to my main class directly, the getPlugin(clazz) method is less efficient and if you happen to send in the wrong class by accident, it will probably break part of your plugin.
     
    Gater12 likes this.
Thread Status:
Not open for further replies.

Share This Page