Pattern · Core

Factory Method

Define a method for creating an object, but let SUBCLASSES decide which concrete class to instantiate — so the creator code never names a product directly.

Asked atAmazonMicrosoftUber
step 1 / 11
1/2Hard-wired new
Dialog
+ render()
Hard-wired new
1class Dialog {
2 void render() {
3 Button b = new WindowsButton(); // hard-coded concrete
4 b.onClick();
5 }
6}
State
createsnew WindowsButton() 💥

Dialog.render() does `new WindowsButton()` directly — the layout class names a concrete widget.