0

Is there any wrong in my Code. Because the variable not changing its value while applying Singleton Pattern.

My Code :

class Singleton{

private final static Supplier<Singleton> INSTANCE = Singleton::new;
private int variable = 33;


public static synchronized Supplier<Singleton> getInstance() { 
    return INSTANCE;
}

void print(){
    System.out.println(variable);
}

void change(int variable){
    this.variable = variable;
}

}

public class Design {

public static void main(String[] args) {
    Singleton.getInstance().get().print(); // print 33
    Singleton.getInstance().get().change(99);
    Singleton.getInstance().get().print(); // need to print 99 but it prints 33

}

}

Please correct me Guys, if I'm wrong in implement Singleton Pattern.

H.Das
  • 225
  • 2
  • 9
  • 5
    You have only one **Supplier**. And each time you call the get() method on the unique Supplier, it creates a **new** instance of the SIngleton class. I sugges you google for "how to implement a singleton in Java". You'll get plenty of relevant answers, and I doubt any of them will look like the one you posted. The easiest, simplest way is to simply create an enum with a single constant. – JB Nizet Nov 24 '19 at 10:19
  • https://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – George Z. Nov 24 '19 at 10:22
  • The code seems to be incomplete, what does `get()` method do ? – Shayan Tabatabaee Nov 24 '19 at 11:25
  • 2
    @ShayanTabatabaee get() calls the Singleton constructor, since the Supplier is initialized to `Singleton::new`. – JB Nizet Nov 24 '19 at 12:02
  • Where did you come up with the idea of using a Supplier for singleton implementation? – ACV Nov 24 '19 at 12:13
  • @JBNizet: Yes it seems that this interface is implemented in java 8, you are right every time the `get` method called a new instance of Singleton class returned. – Shayan Tabatabaee Nov 24 '19 at 13:24

2 Answers2

1

Your Supplier is a Singleton but creates a new instance of your supposed singleton with every call to Supplier.get() as each call invokes Singleton::new returning a new not-singleton instance.

Trying to implement singletons is hard and usually (if not part of a bad homework) sign of a bad design. There is a reason, the 'S' in S.T.U.P.I.D. code stands for Singleton. There only a very few cases where a Singleton is needed and those cases should usually handled by framework code.

Barak
  • 23
  • 6
Nicktar
  • 5,548
  • 1
  • 28
  • 43
0

It is because every call to get method will instantiate a new object and the Singleton design pattern is not implemented well, So there is a tons of tutorials that implemented Singleton design pattern but if you are insist of using Supplier interface I changed your Singleton class and it seems to be work :

import java.util.function.Supplier;

class Singleton implements Supplier<Singleton> {

    private static final Singleton INSTANCE = new Singleton();
    private int variable = 33;

    private Singleton() {

    }

    public static synchronized Singleton getInstance() {
        return INSTANCE;
    }

    void print() {
        System.out.println(variable);
    }

    void change(int variable) {
        this.variable = variable;
    }

    @Override
    public Singleton get() {
        return this;
    }
}