Unravel โบ Java concepts โบ Design patterns and architecture
Factory pattern
A factory creates objects for you, hiding which concrete class is chosen: PaymentFactory.create("card") returns a CardPayment behind a Payment interface.
Learn these first
At a glance
Ordering "a coffee" at a counter: the counter decides whether that means an espresso machine or a filter pot, and you just receive a cup.
- 1A static factory method is the simplest form: Money.of(5, "EUR"), List.of(...), LocalDate.now()They have names, can return a cached or a subclass instance and need not create a new one each time.
- 2A factory with a choice: static Shape create(String type) { return switch (type) { case "circle" -> new Circle(); ..}; }. Adding a new type means editing one place.
- 3Abstract factory: an interface that creates families of related objects (a UI toolkit creating Button and Window for a platform)
- 4In Spring the container is the factory; @Bean methods are factory methods
- A big switch in a factory is still a coupling point; with a registry map (Map<String, Supplier<Shape>>) or a ServiceLoader, new types can be added without changing the factory
- Effective Java prefers static factory methods over constructors for named creation, caching, and returning subtypes
- A factory is nothing special at run time, just a method returning a (possibly different) subtype
public interface Shape { double area(); }class Circle implements Shape { final double r; Circle(double r) { this.r = r; } public double area() { return Math.PI * r * r; } }class Square implements Shape { final double s; Square(double s) { this.s = s; } public double area() { return s * s; } }class ShapeFactory { static Shape create(String type, double size) { return switch (type) { case "circle" -> new Circle(size); case "square" -> new Square(size); default -> throw new IllegalArgumentException("unknown shape " + type); }; }}Read the full explanation (about 1 min)
A factory creates objects for you, hiding which concrete class is chosen: PaymentFactory.create("card") returns a CardPayment behind a Payment interface. Callers depend on the interface only.
A mental model
Ordering "a coffee" at a counter: the counter decides whether that means an espresso machine or a filter pot, and you just receive a cup.
How it works
A static factory method is the simplest form: Money.of(5, "EUR"), List.of(...), LocalDate.now(). They have names, can return a cached or a subclass instance and need not create a new one each time.
A factory with a choice: static Shape create(String type) { return switch (type) { case "circle" -> new Circle(); ... }; }. Adding a new type means editing one place.
Abstract factory: an interface that creates families of related objects (a UI toolkit creating Button and Window for a platform).
In Spring the container is the factory; @Bean methods are factory methods.
Going deeper
A big switch in a factory is still a coupling point; with a registry map (Map<String, Supplier<Shape>>) or a ServiceLoader, new types can be added without changing the factory.
Effective Java prefers static factory methods over constructors for named creation, caching, and returning subtypes. Name them clearly: of, from, valueOf, getInstance, newInstance, create.
Under the hood
A factory is nothing special at run time, just a method returning a (possibly different) subtype. EnumMap and Collections.emptyList() use it to return specialised classes behind one interface.
Common mistakes
- A factory that grows into an unmaintainable switch of dozens of types.
- Returning null for unknown input instead of failing clearly.
- Hiding construction details that callers actually need to control.
How Unravel recognises it
When you paste code, Unravel looks for these signs of factory pattern. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
โฆFactory - Method names you declare:
newInstance()createFor()forType()create()of()from()valueOf()getInstance()newFor()
Part of a learning path
- Design patterns and SOLID ยท previous: Builder pattern ยท next: Strategy pattern
Related concepts
Frequently asked questions
What is Factory pattern?
A factory creates objects for you, hiding which concrete class is chosen: PaymentFactory.create("card") returns a CardPayment behind a Payment interface. Callers depend on the interface only.
What are the most common mistakes with Factory pattern?
A factory that grows into an unmaintainable switch of dozens of types. Returning null for unknown input instead of failing clearly. Hiding construction details that callers actually need to control.
How can I see Factory pattern in real code?
Paste your own Java into Unravel and it will point out where factory pattern appears, explain how it works at the detail level you choose, and draw the flow of the code around it.