JavaIntermediate#design-patterns

What is the Factory design pattern?

A creational pattern that defines a method for creating objects without exposing the exact instantiation logic to the client, letting subclasses or a factory method decide which concrete class to instantiate.

Example
interface Shape { void draw(); }
class Circle implements Shape { public void draw() { System.out.println("Circle"); } }
class ShapeFactory {
  static Shape create(String type) {
    return switch (type) { case "circle" -> new Circle(); default -> null; };
  }
}

Related Questions

1
JavaIntermediate#design-patterns

What is the Builder pattern and when should you use it?

Open
2
JavaIntermediate#design-patterns

What is the Observer pattern?

Open
3
JavaIntermediate#design-patterns

What is the Strategy pattern?

Open