DEV Community

Discussion on: Explain Factory Pattern Like I'm Five

Collapse
 
xowap profile image
Rémy 🤖

Suppose that you want a duck. You don't care much about it, you just want it to quack and nothing else.

To get it you don't build it yourself, you go to the duck factory then ask for a duck and boom you get a duck.

This becomes increasingly useful if there is a city-wide policy on ducks. Suppose that some people need ducks to be of a specific color. When they move in town, the mayor can order ducks to be of the right color. You still don't care because you need them to quack but the new people are happy because they have the right color.

This last paragraph however is a more advanced topic called inversion of control but that fits in the same logic than factories in my opinion (and it's where the principle really shines).

Also suppose you're on the Java island and that everything is boring and painful and limited. Then the factory can relieve some of the pain that you don't find in other places.