0

I am considering using the Singelton Design Pattern and create a Singelton of my Main class. While I was searching, I found some comments that this is rather bad programming practice, especially since the declaration of a static method does not do justice to object-oriented programming. Do you have any suggestions for improving my code?

public class MainClass {
private static MainClass instance = new MainClass();

public static MainClass getMainInstance() {
    return instance;
}

public static void main(String[] args) {
    MainClass main = Main.instance;
}
}
sarath kumar
  • 360
  • 2
  • 15
C-lara
  • 115
  • 8

2 Answers2

1

First of all for a singleton Object implemented on the class must includes the following.

  1. it Should have all the constructor marked private.
  2. it should have the static method having the logic to create the object exactly only one time.
  3. it should have the static reference class variable to hold the single possible instance.

with these we can ensure the Singleton object, but this is not the only way as we can have the creational design pattern or the Fly weight design pattern to overcome the static method call.

Regarding Static keyword, it is still using the Class instance of a object. As we all know any object have the equivalent Singleton class object that gets created on the heap during class loading process. So it is not out of OOPS.

Hope this helps !!!

sarath kumar
  • 360
  • 2
  • 15
  • Hey thank you. However, I would love to know if anything is wrong about my code and whether there are things I could change or make better? – C-lara Apr 19 '20 at 16:57
1

While the use of design patterns often helps in clean programming overusing them in cases where there are not necessary will lead to overcomplicated code.

If you want to create a singleton of your application it would be more beneficial to declare a class or better an enum that contains the application that will be run by your main function.

Using a enum:

public enum Application{
    instance;

    public void run(){
    //do awesome stuff
    }
}

This has the effect that even through serialization the application can not be duplicated but also that you can not generalize your application with interfaces.

When using a normal class to implement a singelton you need to make the constructor private or protect the class otherwise from beeing instanciated again.

Using a normal class with private constructor:

public class Application{
    private static final Application instance = new Application();

    private Application(){}

    public Application getApplication(){
        return instance;
    }

    public void run(){
    //do awesome stuff
    }
}

This variant has the advantage that the class can still implement interfaces or extend classes e.g. Runnable. The disadvantage would be that through the use of serialization the class can still be instanciated multiple time.

vunop
  • 66
  • 5
  • Hey, thank you. That helps. However, I would prefer using Singleton. So how can I prevent serialization? – C-lara Apr 19 '20 at 17:11
  • Using an enum you dont (look also: https://stackoverflow.com/q/3930181/5732150) – vunop Apr 19 '20 at 17:17