Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong when not using singleton pattern

I'm reading about the singleton pattern from my ebook (Head First Design Patterns), and I know that using this pattern is suitable in case you need only one instance of some class.
But I have a little trouble with the problem introduction in this ebook.
(Yeah, I think I can quote part of it here !?)

The Chocolate Factory
Everyone knows that all modern chocolate factories have computer controlled chocolate boilers. The job of the boiler is to take in chocolate and milk, bring them to a boil, and then pass them on to the next phase of making chocolate bars.
Here’s the controller class for Choc-O-Holic, Inc.’s industrial strength Chocolate Boiler. Check out the code; you’ll notice they’ve tried to be very careful to ensure that bad things don’t happen, like draining 500 gallons of unboiled mixture, or filling the boiler when it’s already full, or boiling an empty boiler!

public class ChocolateBoiler {
    private boolean empty;
    private boolean boiled;

    private ChocolateBoiler() {
        empty = true;
        boiled = false;
    }

    public void fill() {
        if (isEmpty()) {
            empty = false;
            boiled = false;
            // fi ll the boiler with a milk/chocolate mixture
        }
    }

    public void drain() {
        if (!isEmpty() && isBoiled()) {
            // drain the boiled milk and chocolate
            empty = true;
        }
    }

    public void boil() {
        if (!isEmpty() && !isBoiled()) {
            // bring the contents to a boil
            boiled = true;
        }
    }

    public boolean isEmpty() {
        return empty;
    }

    public boolean isBoiled() {
        return boiled;
    }
}

Yeah, and this is their question:

Choc-O-Holic has done a decent job of ensuring bad things don’t happen, don’t ya think? Then again, you probably suspect that if two ChocolateBoiler instances get loose, some very bad things can happen.
How might things go wrong if more than one instance of ChocolateBoiler is created in an application?

So, the problem will "happen" when we do this:

ChocolateBoiler boiler1 = new ChocolateBoiler(),
 boiler2 = new ChocolateBoiler();
//...

But I see that these two instances control its own behavior, and they run independently (because no static field here).
So they run separately without effect to the others.
I wonder that this problem is about illegal state or something might happen when one instance run and effect to the others ("incorrect program behavior, overuse of resources, or inconsistent results", from ebook), but it is not here

So, How might things go wrong here?, is it just about wasteful instance?

if two ChocolateBoiler instances get loose, some very bad things can happen.

I want to see how that bad things happen?

#Edit 1: Thank everyone for helping me. I figured out what is my problem,
When I call boiler2 = new ChocolateBoiler(), the boiler2 instance still refers to the same boilder as bolder1, is it?
The first time I think new ChocolateBoiler() is similar to buy a new boiler :)
This is about the conception, and I am a newbie here

like image 959
phibao37 Avatar asked Dec 14 '22 14:12

phibao37


1 Answers

You seem not to understand the concept that this example is trying to explain. ChocolateBoiler is not a real boiler, it's a java class.

However, this class could be used to instruct a piece of hardware (a real boiler controller) to execute some actions. If you mistakenly had two instances of ChocolateBoiler and you mistakenly use both of them to instruct the same boiler controller, then obviously you're in trouble.

There are two 'mistakenly' in my previous paragraph, and you may argue that if you do things 'mistakenly' in general, then you're in trouble anyway. But in case of badly designed singletons, mistakes may not be so obvious. If you serialize and deserialize a singleton that does not handle serialization concerns to keep uniqueness, and then try to use that instance to heat the boiler, you could burn the boiler down.

like image 152
Dragan Bozanovic Avatar answered Dec 27 '22 11:12

Dragan Bozanovic