public static Polygon polygonFactoryMethod(final int numberOfSides) { if (numberOfSides == 3) { return new Triangle(); } else if (numberOfSides == 4) { return new Square(); } else { throw new RuntimeException("invalid input"); }}
Creating objects of SAME type but with Different states
class Polygon { private String type; public Polygon(final String type) { this.type = type; } public String getType() { return type; }}
public static Polygon polygonFactoryMethod(final int numberOfSides) { if (numberOfSides == 3) { return new Polygon("triangle"); } else if (numberOfSides == 4) { return new Polygon("square"); } else { throw new RuntimeException("invalid input"); }}