Show de boa! Adaptei a factory removendo os ifs: public class PolygonFactory { private static final Map polygons = Map.ofEntries( entry(3, new Triangle()), entry(4, new Square()), entry(5, new Pentagon()) ); public static Polygon createPolygon(int numberOfSides) { if (!polygons.containsKey(numberOfSides)) { throw new IllegalArgumentException("Invalid number of sides!"); } return polygons.get(numberOfSides); } }