DEV Community

Discussion on: Explain Factory Pattern Like I'm Five

alephnaught2tog profile image
Max Cerrina

It's 12am. The pipes in your house break. There is water everywhere. You are worried about property damage.

You call Pick-Your-Own-Plumber.
The operator asks you what your pipes are made of.
You don't know.
The operator asks you how many pipes you have.
You don't know.
The operator says: I can't give you a plumber to fix your pipes, if you can't tell me how to fix them.
Needless to say, you hang up, angry.

You call Plumbers-For-All-Occasions.
The operator asks you what's wrong.
You say: I don't know and I don't care, my pipes are broken. All I need is someone who can fix my pipes -- I don't care how, I don't care whether they do it with geometry or magic, I JUST NEED SOMEONE TO FIX MY PIPES.
The operator says OK, we will send someone to fix your pipes.

The plumber comes.
He looks at your pipes, and based on what he sees, he does the right thing.
He wasn't sure exactly WHAT he would do to fix them until he got there, but he knew he COULD fix them, because pipe fixing is something he knows how to do.
He decided HOW to fix them once he got there and not before.

Plumbers-For-All-Occasions is a factory.

akopchinskiy profile image

This kind of examples are good for real life, but not for programming.

alchermd profile image
John Alcher

Plumbers-For-All-Occasions is an awesome company name.

alephnaught2tog profile image
Max Cerrina

brb trademarking