Factory is a pattern that encapsulates object creation logic. Instead of new ConcreteClass() scattered throughout the code, you call a factory that decides which class to instantiate. The client code does not know how the object is created.
Variants
Simple Factory — a static method returning the right object based on a parameter
Factory Method — a base class defines the creation interface; subclasses override the concrete implementation
Abstract Factory — a factory of factories: creates families of related objects
When useful
The object type depends on runtime conditions or configuration
Complex initialisation logic needs to be isolated in one place
Easy to swap the implementation without changing client code