Factory Method creates one kind of object. Abstract Factory creates a family of related objects that must match each other.
Factory Method defines a method that creates one kind of object. Subclasses decide which concrete class to return. In practice, this is often an implementation of an interface, not a subclass. Example: an abstract ReportExporter has a createWriter() method. CsvExporter returns a CSV writer.
Abstract Factory is an interface for creating families of related objects. The caller never names the concrete classes. The factory guarantees that the products match each other.
Backend example: a CloudFactory has three methods:
createBlobStore()createQueue()createSecretStore()
AwsFactory returns adapters for S3, SQS and Secrets Manager. GcpFactory returns the Google Cloud versions of the same things. The app picks one factory at startup. Because of this, it never mixes an AWS queue with a GCP store.
An Abstract Factory is often built from several factory methods. A plain function that switches on a config value is called a simple factory. It is a common idiom, but it is not one of the GoF patterns (the 23 patterns in the "Gang of Four" Design Patterns book).
- Factory Method creates one product, and the implementation decides the concrete class.
- Abstract Factory is an interface for creating a family of related products.
- Abstract Factory keeps the products consistent with each other.
- A simple factory function is a common idiom, not a GoF pattern.