-1

I don't really understand the concept of singleton in my bukkit plugin. In my plugin I need to get the plugin data folder and change the json file. I have to use the functionthis.getDataFolder().getAbsolutePath(); but it cannot be used in my plugin because I use static functions. I already tried to do something like this:

@Override
public void onEnable() {
   File file = getFile();
}

public static getFile() {
   return this.getDataFolder().getAbsolutePath();
}

I use static functions because my plugin is divided in multiple files.

Raffon
  • 133
  • 10
  • 2
    That isn't (quite) legal Java code. There are **many** ways to implement a `Singleton`, `enum` seems to be the most popular these days; why don't you research "Java Singleton" – Elliott Frisch Feb 08 '20 at 07:42
  • Go through this - https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples – Ashutosh Feb 08 '20 at 07:44
  • 1
    Generally speaking, you shouldn't use language singletons for anything that has state; it causes far too many problems. Singletons should be reserved for things like implementations of mathematical formulas that never vary. In the case that a singleton really makes sense, use the enum approach. – chrylis -cautiouslyoptimistic- Feb 08 '20 at 09:14

3 Answers3

1

Best way to implement singleton pattern in java is via ENUMS, you can refer the code below for the same:

Enum Class:

public enum SingletonEnum {
    INSTANCE;
    int value;
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
}

Calling class:

public class EnumDemo {
    public static void main(String[] args) {
        SingletonEnum singleton = SingletonEnum.INSTANCE;
        System.out.println(singleton.getValue());
        singleton.setValue(2);
        System.out.println(singleton.getValue());
    }
}
Shivam Mohan
  • 397
  • 3
  • 14
1

This is the singleton pattern, you can search for google. ex :

public final class Singleton {
    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return INSTANCE;
    }
}
陳昇昇
  • 35
  • 5
1

Following is a demonstration of using Singleton pattern in java. It is using Lazy Initialization, so that the singleton instance is only allocated on-call.

The idea behind singleton pattern is to basically ensure that a class has only one instance, while providing a global access point to this instance.

And to achieve that, the basic methodology is to keep the constructor private, while exposing a public method for getting the singleton instance of that class.

import java.util.*;
import java.lang.*;
import java.io.*;

class Box
{
    private int x,y,z;
    private static Box instance;

    private Box(){
        x=y=z=2;
    }

    public static Box getSingleTonInsnace(){
        if(instance == null){
            instance = new Box();
        }
        return instance;
    }
    public String toString(){
        return String.format("Box with volume = %d", x*y*z);
    }

}

public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Box box = Box.getSingleTonInsnace();
        System.out.println(box);
    }
}

You can also view this link to know more about other ways to use singleton pattern in java.

FahimAhmed
  • 497
  • 3
  • 14