"The Factory Method pattern is a design pattern used to define a runtime interface for creating an object. It's called a factory because it creates various types of objects without necessarily knowing what kind of object it creates or how to create it."
The method createCircle was used to check the validity of the input parameters and prevent the creation of invalid objects.
static Circle createCircle(Point centre, double radius){
if (radius > 0)
return new Circle(centre, radius);
else
return null;
}
Note: Factory methods is static.
Static methods belongs to a class instead of an instance of the class.
Static methods is a form of encapsulate object creation.
According to my understanding, factory method is defined as static so that it can be called without the need of creating an instance.
private static final BOX EMPTY_BOX = new BOX();
public static Box empty() {
return EMPTY_BOX;
}
Please feel free to edit if I have made any mistakes or add on more content.😄