For a long time, the Factory pattern felt like ceremony to me — an extra class that did nothing my code couldn't already do. Then I thought about ten people and a pen, and it finally clicked. Let me walk you through it.
Ten people, ten pens
Imagine ten people in an office. All of them need to write something. So you give them an instruction:
"Make your own pen, fill it with blue ink, and start writing."
Everyone follows it. Ten people build a pen, fill it with blue, and get to work. In code, each of them is doing this:
Pen pen = new Pen("blue"); // I build it myself
pen.write();
It works. Everyone is writing. Life is good — until the requirement changes.
"Actually, make it red"
Now the instruction changes: stop using blue, use red.
What happens? Every one of the ten people has to personally go to the market, buy red ink, come back, and refill their pen. Ten trips. Ten refills. And here's the dangerous part — if even one person forgets, they keep writing in blue. Nobody notices until the page comes out wrong.
// Every single person must now find and change this line
Pen pen = new Pen("red");
Look at what one small change just cost you:
- Ten separate trips to the market — repeated effort.
- Ten chances to forget — repeated risk of error.
- No easy way to confirm everyone actually switched.
This is the real-world pain. The change itself (blue to red) is tiny. The problem is that the knowledge of how to make a pen is copied into ten heads, so changing it means disturbing all ten.
The pen-maker
So you do something simple. You appoint one person — the pen-maker — and give them the job of building pens. Now nobody makes their own pen. Everyone just walks up and says:
"Give me a pen."
// The pen-maker — the only one who knows how a pen is built
class PenMaker {
Pen givePen() {
return new Pen("blue");
}
}
// Each of the ten people now simply asks
Pen pen = penMaker.givePen(); // "give me a pen"
pen.write();
The pen-maker is the Factory. That's the whole pattern. It isn't a clever trick — it's just one person who holds the knowledge of how to build the thing, so everyone else can stop holding it.
Now watch the same change
Blue to red, one more time. Where does the change happen now?
class PenMaker {
Pen givePen() {
return new Pen("red"); // the only line that changes
}
}
One line. One place. The pen-maker switches to red, and from that moment everyone who asks for a pen gets red — without going anywhere, without changing anything, without even knowing it happened. They still just say "give me a pen."
Ten trips became one. Ten chances to forget became one. That is what the Factory pattern buys you.
The full picture, both ways
Snippets are easy to nod along to, so here is the whole thing written out — all ten people, both versions — so you can count the change sites with your own eyes.
First, the pen itself. This part is the same in both versions:
// A pen is built with one ingredient: the ink colour.
class Pen {
private String inkColor;
Pen(String inkColor) {
this.inkColor = inkColor;
}
void write() {
System.out.println("Writing in " + inkColor);
}
}
Without a pen-maker
Every person builds their own pen, so the colour "blue" is typed into all ten places:
class Office {
void person1() { Pen p = new Pen("blue"); p.write(); }
void person2() { Pen p = new Pen("blue"); p.write(); }
void person3() { Pen p = new Pen("blue"); p.write(); }
void person4() { Pen p = new Pen("blue"); p.write(); }
void person5() { Pen p = new Pen("blue"); p.write(); }
void person6() { Pen p = new Pen("blue"); p.write(); }
void person7() { Pen p = new Pen("blue"); p.write(); }
void person8() { Pen p = new Pen("blue"); p.write(); }
void person9() { Pen p = new Pen("blue"); p.write(); }
void person10() { Pen p = new Pen("blue"); p.write(); }
}
Now change blue to red. You must find and fix all ten lines:
class Office {
void person1() { Pen p = new Pen("red"); p.write(); } // changed
void person2() { Pen p = new Pen("red"); p.write(); } // changed
void person3() { Pen p = new Pen("red"); p.write(); } // changed
void person4() { Pen p = new Pen("red"); p.write(); } // changed
void person5() { Pen p = new Pen("red"); p.write(); } // changed
void person6() { Pen p = new Pen("red"); p.write(); } // changed
void person7() { Pen p = new Pen("red"); p.write(); } // changed
void person8() { Pen p = new Pen("red"); p.write(); } // changed
void person9() { Pen p = new Pen("red"); p.write(); } // changed
void person10() { Pen p = new Pen("red"); p.write(); } // changed
}
Ten edits. Miss one, and that person quietly keeps writing in blue.
With a pen-maker
The colour is typed in exactly one place — inside the pen-maker. Nobody else mentions it:
// The pen-maker — the only one who knows how a pen is built.
class PenMaker {
Pen givePen() {
return new Pen("blue"); // the single source of the colour
}
}
class Office {
private PenMaker penMaker = new PenMaker();
void person1() { Pen p = penMaker.givePen(); p.write(); }
void person2() { Pen p = penMaker.givePen(); p.write(); }
void person3() { Pen p = penMaker.givePen(); p.write(); }
void person4() { Pen p = penMaker.givePen(); p.write(); }
void person5() { Pen p = penMaker.givePen(); p.write(); }
void person6() { Pen p = penMaker.givePen(); p.write(); }
void person7() { Pen p = penMaker.givePen(); p.write(); }
void person8() { Pen p = penMaker.givePen(); p.write(); }
void person9() { Pen p = penMaker.givePen(); p.write(); }
void person10() { Pen p = penMaker.givePen(); p.write(); }
}
Now change blue to red. There is only one line in the whole program that even mentions the colour:
class PenMaker {
Pen givePen() {
return new Pen("red"); // the only line that changes
}
}
The ten people are not touched. They never said "blue," so there is nothing in them to find, fix, or forget. Ten edits became one.
The honest part nobody tells you
The Factory does not delete the work. Somebody still has to change blue to red. The pen-maker still makes that change. The market trip doesn't vanish — it just gets made once, by one person, instead of ten times by ten people.
So if a colleague says, "but the pen-maker still had to change it!" — they're right, and that's not a flaw. Your answer is simply: yes, once, instead of ten times. That's the entire point.
The one sentence to remember
A Factory is a pen-maker: one place that holds the knowledge of how to build something, so that when the way of building it changes, only that one place has to relearn — and everyone who just needs a finished pen is never disturbed.
Top comments (0)