Singleton Model - Doubt about Head First Design Patterns

See page 175 for an example of the Chocolate Boiler class. Something like that:

public class ChocolateBoiler { private boolean empty; private boolean boiled; public ChocolateBoiler { empty = true; boiled = false; } // and then three methods to fill, drain and boil which changes the // status of these two flag depending of situation } 

In the “Power of the brain” section, they ask the question “How can things go wrong if several ChocolateBoiler instances are created in the application?”

I am not sure what the problem is with this class. Why are we introducing a singleton pattern here? These two flags are not static and therefore a single instance. So how to create more than one instance, can ruin things?

+6
oop singleton
source share
4 answers

The issue is not creating an instance of the object.

This is about confusion caused by the presence of two instances of the object, both of which have ChocolateBoiler status.

If an object (for example, Cooker) considers it to have ChocolateBoiler status and some other objects (for example Recipe) in which it has ChocolateBoiler status, what happens now?

Because variables are instance variables, ChocolateBoiler objects are not consistent with ChocolateBoiler status. Now what is happening?

+2
source share

This is only a problem if there can be only one ChocolateBoiler, and if it can be only one, it should be a single.

+2
source share

I believe that in this example you had only ONE chocolate pot. And therefore, you should be able to create only one instance of the object representing it. If you were allowed to create multiple instances, you could then issue the command if (boiler.hotEnough()) boiler.stop() somewhere on your system and would be surprised that although the boiler is already too hot, it does not stop , because you are talking to some kind of "dead" instance of Kotel, which returns hotEnough() : false.

Using a singleton template, you will make sure that no matter where in your code you say that Boiler.getInstance () you will get the only and only boiler object, and when you talk to it, it will do what you expect.

+1
source share

The whole example of the singlet chocolate ice cream really bothered me when I read it.

At a truly fundamental level, I don’t understand why this is necessary when you have only one physical thing to ensure this fact in the software. What happens if you get another one? what are you going to do, add the second to the same singleton? make 2 different singleton? a simple global variable would do the job.

IMO, this is not the boiler itself, from which you can have only one, its access to this particular boiler controller. You cannot allow the second person to start making a new batch of chocolate while he is already in the process for someone else, or even let the same person make the second batch before the first is finished. From this point of view, a simple queuing or batch processing system would do the job. Using another sample from the book, the team template will be a much better way of processing, as there is only one waitress, and all new orders will be queued until the cook makes a meal with the current order. (er, if you have not seen the book, what I just said may not make sense, sorry)

Maybe I just don't get it. Because of this, I did not do a lot of OOP or anything with design templates, and I lose the opportunity to work, so I read about it.

+1
source share

All Articles