0
void build(){
  while(true){
    Builder builder = new Builder();
    ...
  }
}

class Builder{
   private final Store store = null;

   public Builder(){
     store = Store.getInstance();
   }
}

The 'Store' is implemented as a standard singleton class, as shown here: https://en.wikipedia.org/wiki/Singleton_pattern

I hope the Store object will be initialized only once and in the 'while' loop the same 'store' object will be created and returned inside the multiple "builder" objects due to the whole loop. Is this guaranteed? What about if defining "store" as a static variable of Builder?

user697911
  • 10,043
  • 25
  • 95
  • 169
  • If `Store` is a singleton, why do you even need to save an instance of it? Why not have your code use `Store.getInstance()` directly? – Mureinik May 25 '17 at 17:51
  • Why not just put the `Builder builder = new Builder();` on the outside of the `while` loop. And if needed, on the inside do `builder = new Builder();` on the inside? Or is this a puzzle question? – Josh Heaps May 25 '17 at 17:51
  • 1
    If it's a singleton *object*, it doesn't matter how many *references* you have to it. Also, note that since Java 5, a singleton can be implemented by a single-element enum. – Andy Thomas May 25 '17 at 17:54
  • @Mureinik In this case it's not any better but if dependency injection were used, there's plenty of reasons for not accessing the singleton directly. First and foremost, testability. – Michael May 25 '17 at 17:54
  • @Mureinik, because in my same class, I need to refer to the Store object more than 1 time. By storing it as one variable, I don't have to write "Store.getInstance()" multiple times. Does this make sense? – user697911 May 25 '17 at 18:00
  • @JoshHeaps, my code is incomplete, actually there is a parameter to the Builder's constructor and I omitted here. That is why it has to be in the loop. – user697911 May 25 '17 at 18:06
  • Your class `Builder` won't compile as presented. Did you try to compile it? You cannot assign to a `final` variable but once. – Lew Bloch May 25 '17 at 21:29
  • @user697911 I don't agree that avoiding ​multiple calls to `getInstance` makes any sense. Then again, I don't agree that a singleton should have a `getInstance` method anyway. – Lew Bloch May 25 '17 at 21:31

2 Answers2

1

I hope the Store object will be initialized only once and in the 'while' loop the same 'store' object will be created and returned inside the multiple "builder" objects due to the whole loop. Is this guaranteed?

Yes, there will be a single instance of the Store object if it is implemented as a Singleton and this is due to the keyword static.

What about if defining "store" as a static variable of Builder?

You may or may not do this depending on your needs but if you really need a Singleton (which in many cases you don't), you may want to store it in a separate class, just to make the code more structured.

Also, you don't need private final Store store = null;, as you may always access (the same instance) of Store using Store.getInstance().

syntagma
  • 23,346
  • 16
  • 78
  • 134
0

Because the instance of Store is static within its class, calling getInstance() should not create a new Store. getInstance() will simply return you the private static final Store instance that you should have inside your singleton.

From the wikipedia page:

An implementation of the singleton pattern must: ensure that only one instance of the singleton class ever exists; and provide global access to that instance.

Chris Gilardi
  • 1,509
  • 14
  • 26