ThisCalculator

Everyday

Factory Method vs Abstract Factory: How to Choose

Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered

Factory Method vs Abstract Factory: How to Choose

Choose the Factory Method when you need to create one product through subclasses, and choose the Abstract Factory when you need to create families of related products without specifying their concrete classes. The Factory Method relies on inheritance—a subclass decides which class to instantiate—while the Abstract Factory relies on composition—a separate factory object creates the products. This article explains both patterns, compares them directly, and gives concrete rules for deciding which one fits your code.

Formula

The calculator above is not part of the design-pattern discussion; it is a pace calculator included as a page element. The formula for average pace is:

pace = finish time / distance

where pace is in minutes per kilometer, finish time is in minutes, and distance is in kilometers. The average speed is distance divided by time in hours, so speed = distance / (finish time / 60), giving kilometers per hour.

Worked Example

Using the default values: finish time = 50 minutes, distance = 10 km.

Average pace = 50 / 10 = 5.00 min/km.

Average speed = 10 / (50 / 60) = 10 / 0.8333 = 12.00 km/h.

The calculator shows 5.00 min/km and 12.00 km/h for those inputs.

Background: What Are These Patterns?

Both Factory Method and Abstract Factory are creational design patterns from the classic “Gang of Four” book. They solve the problem of creating objects without tying your code to specific classes. The key difference is the purpose: Factory Method creates a single product, while Abstract Factory creates a whole family of products that are designed to work together.

Factory Method is the simpler of the two. You define an interface for creating an object, but let subclasses decide which class to instantiate. The classic example is a logistics app that ships by truck or ship. The base class has a method createTransport(), and each subclass overrides it to return the right type.

Abstract Factory goes further. It provides an interface for creating families of related objects without specifying their concrete classes. The classic example is a GUI toolkit that supports Windows and Mac. The abstract factory has methods like createButton() and createCheckbox(). Each concrete factory (WindowsFactory, MacFactory) returns the correct widgets for that platform.

How Factory Method Works

Factory Method uses inheritance. You have a base class (often abstract) with a factory method. That method returns an object of a product type. Subclasses override the factory method to return different concrete products.

  • Creator – the base class with the factory method. It may contain core logic that uses the product.
  • ConcreteCreator – a subclass that overrides the factory method to return a specific product.
  • Product – the interface or abstract class that all created objects must implement.
  • ConcreteProduct – the actual class that gets instantiated.

Example: a document editor that creates different types of documents. The base class Application has a method createDocument(). The subclass TextApplication overrides it to return a TextDocument. The core logic in Application calls createDocument() and works with it through the Document interface, so it never knows which concrete class it got.

How Abstract Factory Works

Abstract Factory uses composition. You create a separate factory object that is responsible for creating products. The client code receives a factory and calls its methods to get products. The factory object is passed in, often through a constructor or a setter.

  • AbstractFactory – declares creation methods for each product in the family.
  • ConcreteFactory – implements those methods to return specific product variants.
  • AbstractProduct – an interface for each type of product (e.g., Button, Checkbox).
  • ConcreteProduct – the actual class (e.g., WindowsButton, MacCheckbox).
  • Client – uses only the abstract factory and abstract product interfaces.

Example: a furniture store that sells Victorian and Modern styles. The FurnitureFactory interface has createChair() and createSofa(). The VictorianFactory returns Victorian chairs and sofas; the ModernFactory returns modern ones. The client code works with the factory interface, so it can switch styles by swapping the factory object.

How to Choose: Decision Rules

Ask yourself these questions in order:

  1. Do you need to create one product, or several related products? If it’s just one product, use Factory Method. If you need multiple products that must match (like buttons and checkboxes for the same OS), use Abstract Factory.
  2. Do you want to let subclasses decide? If you have a class hierarchy and want each subclass to provide its own product, Factory Method is natural. If you want to pass a factory object around and swap it at runtime, Abstract Factory is better.
  3. Do the products need to be used together consistently? Abstract Factory guarantees that the products from one factory are compatible. Factory Method does not enforce any relationship between different products.
  4. Is the number of product types stable? If you often add new product types, Factory Method is easier to extend (just add a new subclass). Abstract Factory requires changing the abstract factory interface and every concrete factory, which is more invasive.

In short: Factory Method is for a single product, Abstract Factory is for a family of products. Use Factory Method when you have a class hierarchy and want to delegate creation to subclasses. Use Abstract Factory when you have multiple product types that must be used together and you want to swap whole families at once.

Common Problems and Misconceptions

A frequent misconception is that Abstract Factory is always “better” because it’s more advanced. That’s not true. Abstract Factory adds complexity—you need factory interfaces, concrete factories, and more classes. If you only need one product, that complexity is wasted.

Another mistake is using Factory Method when the product family is large and tightly coupled. If you have 10 product types and they must match, Factory Method would force you to create 10 separate factory methods and coordinate them, which is messy. Abstract Factory handles that cleanly.

Also, do not confuse Factory Method with the simple Factory idiom (a static method that returns different objects based on a parameter). That idiom is not a GoF pattern; it’s a common technique but it doesn’t support subclassing.

Comparison: Factory Method vs Abstract Factory

Aspect Factory Method Abstract Factory
Purpose Create one product Create a family of related products
Mechanism Inheritance (subclass overrides) Composition (factory object passed in)
Number of products One Multiple, related
Client code Works with Creator subclass Works with AbstractFactory interface
Extending with new product type Add a new subclass Change interface and all factories
Guarantees product compatibility No Yes, within one factory
Runtime swapping Harder (you change subclass) Easy (swap factory object)

In practice, you can combine them: an Abstract Factory can be implemented using Factory Methods. Each concrete factory’s creation method is often a Factory Method.

Costs and Trade-offs

Factory Method costs you one extra class per product variant, but it keeps the client code simple and decoupled. The main cost is that you must create a subclass for each product type, which can lead to many small classes.

Abstract Factory costs more upfront: you need factory interfaces, concrete factories, and often a way to select the right factory. The benefit is that you can swap entire families at runtime and you get consistency guarantees. The cost is higher complexity and more boilerplate.

Neither pattern is free. Always consider whether you actually need the flexibility. If you only have one product and no plans to add more, a simple constructor call is better than any pattern.

Alternatives and Related Patterns

  • Simple Factory – a static method that returns different objects based on a parameter. Easy but not a GoF pattern; not extensible via subclassing.
  • Prototype – create new objects by cloning an existing one. Useful when creation is expensive or complex.
  • Builder – construct complex objects step by step. Use when you need to build an object with many configuration options.
  • Singleton – often used with Abstract Factory to ensure only one factory instance exists.

For example, if you need to create a complex object like a house with many parts, Builder is more appropriate than Factory Method. If you need to create objects that are similar but not identical, Prototype might be simpler.

Frequently Asked Questions

Can I use both Factory Method and Abstract Factory together?

Yes. Many real-world implementations use Abstract Factory as the top-level interface, and each concrete factory uses Factory Method to create individual products. This combines the family-level consistency of Abstract Factory with the subclassing flexibility of Factory Method.

Is Factory Method simpler than Abstract Factory?

Yes, generally. Factory Method involves one method and a few subclasses. Abstract Factory involves multiple methods, multiple product interfaces, and multiple concrete factories. If you only need one product, Factory Method is the simpler and more appropriate choice.

Which pattern should I use for dependency injection?

Abstract Factory is often used with dependency injection because you can inject a factory object that creates the needed products. Factory Method is less common in DI because it relies on subclassing, which is harder to swap at runtime.

What is the difference between Factory Method and a simple factory?

A simple factory is a single function or class that uses a condition to return different objects. It is not a GoF pattern and does not use inheritance. Factory Method uses subclasses to override a creation method, allowing each subclass to provide its own product.

When should I avoid these patterns?

Avoid them when you have only one product type and no plans to add more. The patterns add indirection and extra classes. Also avoid Abstract Factory if you frequently add new product types, because that requires changing the factory interface and all concrete factories.

Does Abstract Factory guarantee that products are compatible?

Yes, within a single concrete factory. If you use a WindowsFactory, it creates WindowsButton and WindowsCheckbox, which are designed to work together. This is the main advantage over using Factory Method separately for each product.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.