Posted Joe Chu misc4 minutes read (About 613 words)0 visits
Factory Method vs Abstract Factory
Both belong to creational design pattern.
1. Introduction
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
Here is a greate analogy to understand the difference:
Imagine you are running a factory that produces paintings.
Factory Method: “One Artist, One Painting Style”
You have different artists (subclasses), and each artist specializes in one type of painting (product).
The base class provides a method createPainting(), and each artist (subclass) decides what kind of painting they create.
Example:
A Landscape Artist creates landscape paintings.
A Portrait Artist creates portrait paintings.
If you add a Modern Artist, they create modern paintings. Key Idea: The factory method lets subclasses decide which single product to create.
Abstract Factory: “A Complete Art Studio”
Now, instead of just one type of painting, you need to provide a full art experience—paintings, frames, and brushes.
You create a factory for each style of art, and each factory produces a family of related products.
Example:
A Landscape Art Studio produces:
Landscape Paintings
Wooden Frames
Oil Brushes
A Modern Art Studio produces:
Abstract Paintings
Metal Frames
Acrylic Brushes
Key Idea: The abstract factory ensures that related products are created together.