0

I have a singleton class and I added the msg method but I'm not sure if is okay to add it to the singleton class or not

public class Singleton {
    private static Singleton single_instance = null;
    private Singleton() {
       
    }

    public void msg(String msg){  
        System.out.println(msg);
    } 

    public static Singleton getInstance() {
        if (single_instance == null) {
            single_instance = new Singleton();
        }

        return single_instance;
    }
}
hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • Why would you think it would not be safe? – hfontanez Feb 10 '22 at 16:56
  • I just want to make sure if it correct and it will still be a singleton class – user10591778 Feb 10 '22 at 17:04
  • To invoke msg you must have an instance o Singleton. What is the problem exactly? – pringi Feb 10 '22 at 17:06
  • Does this answer your question? [Java Singleton Design Pattern : Questions](https://stackoverflow.com/questions/3428615/java-singleton-design-pattern-questions) – pringi Feb 10 '22 at 17:07
  • @pringi No, that does not answer the OP question. Did you even read your suggested question? Nowhere there it directly addresses the the OP question about being OK or not to have other methods in the Singleton class. – hfontanez Feb 10 '22 at 17:11
  • It explains what is the singleton pattern. If one understands the pattern then it can answer this question. – pringi Feb 10 '22 at 17:15

1 Answers1

1

Singleton is a Creational Design Pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Aside from that, it is just an ordinary class that can have as many public methods as will need to satisfy the requirements for which it was designed.

Singleton objects can mutate (change the value of its internal attributes - variables), or can be immutable (for example, Java enums can be used to create Singleton instances).

Using your class as an example, you can do something like this and be perfectly legal:

public class Singleton {
    private static Singleton single_instance = null;
    private String message = "none";
    private Singleton() {
       
    }

    public void showCurrentMessage(){  
        System.out.println(msg);
    }
    public void updateMessage(String msg) {
        this.msg = msg;
    }

    public static Singleton getInstance() {
        if (single_instance == null) {
            single_instance = new Singleton();
        }

        return single_instance;
    }
}

Mind you that we are strictly speaking of the legality of this approach. Another discussion to be had is regarding the practicality of the approach where you need to consider many things like:

  1. Testability of the code - one of the reasons why Singleton are not liked very much
  2. Usability in multithreaded applications - Many safeguards are needed when objects mutate in multithreaded context.

There could be many others; albeit some of those (if not all) apply to non-Singleton objects as well.

hfontanez
  • 5,774
  • 2
  • 25
  • 37