1.14.4 Kinda like singleton

Discussion in 'Spigot Plugin Development' started by swendel, Mar 9, 2020.

  1. Is there a way to save data to a class from a string kind've like a singleton but not, for example:

    Shop shop = new Shop("test");
    shop.addItem(foo);

    then if i did that again it would retain the data i set in it until the server restarts like this

    Shop shop = new Shop("test");
    shop.getitem();
    returns foo

    if you don't understand i'll reedit this
    thank you
     
  2. I'm not entirely sure what you're asking. If you want to convert an object's fields into a string and then save that, I'd look into serialization, which allows you to save objects to files and then recreate them from files later. If you're looking to construct an instance of a class from a string, that's completely dependent on what you're trying to do but you'll need a constructor. Neither of these have anything to do with the singleton pattern, however, which allows only a single instance of a particular class.

    The example you posted is a bit confusing - the "addItem" function implies that you have a list of items, to which you are adding, but the "getItem" function implies that there is only one item. To be semantically correct, you could either change "addItem" to "setItem" (so there's one item) or "getItem" to "getItems" (so there is more than one item) and then update the implementation accordingly.

    Feel free to clarify if this doesn't answer your question.
     
  3. sorry i explained that badly, i don't think anything you stated is what i'm looking for. thank you though

    i want to save an object to an instance of a class with a name as a string, and if i make a new instance of that class with the same name as a new instance, all the previous data is still there. please tell me if you need more information

    thank you!
     
  4. drives_a_ford

    Moderator

    You're going about this all wrong.
    Whenever you create a new instance, then that's what it is - a new instance.

    What you actually want to do is create another class that handles all the shops. This class has a getShop method that takes a string argument and returns a shop. If there was previously no shop by the name you specified, a new one will be created and put in a map within the class. If there already was a shop by the name, that one will be returned.
     
  5. FrostedSnowman

    Resource Staff

    In addition to what was said before, this is a common pitfall that leads you to static abuse. Every time you instantiate a new object, it should be a new object. If you're looking for save or persist data during the program's lifetime, you'll need to cache it in a collection or similar. For example:

    Code (Java):
    private final Map<String, Shop> shops = new HashMap<>();

    public Shop getShop(String shop) {
         return this.shops.get(shop);
    }
    A singleton pattern is something entirely different than what you described. The idea of a singleton is to ensure that there is only one instance of that object exists in the jvm. This is more of a design pattern that has uses such as: logging, driver objects, caching and thread pooling; not necessarily singular objects as you described.

    Feel free to research more on the singleton pattern, you'll see what it's about :)
     
    • Agree Agree x 2