cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 167 of 218
Ideas this builds on
Low-level design167 / 218

Abstract Factory: build compatible families

Picture

A text interface wants text buttons and checkboxes. A symbol-based interface wants matching symbolic versions.

Predict

What does Abstract Factory add beyond creating one object?

Walk through the small example

A compatible product family. Product compatibility is the additional promise.

Mental model and complete Python solution

Two factories expose the same button and checkbox operations. render_form depends on that family interface. Strings keep the teaching example small; real products can be objects with behavior. Adding a family is localized, while adding a new product category affects every family.

What the function promises: Return matching string representations; this is not a UI framework implementation.

class TextFactory:
    def button(self, label):
        return f"[{label}]"
    def checkbox(self, checked):
        return "[x]" if checked else "[ ]"

class SymbolFactory:
    def button(self, label):
        return f"({label})"
    def checkbox(self, checked):
        return "☑" if checked else "☐"

def render_form(factory):
    return factory.button("Save") + " " + factory.checkbox(True)

assert render_form(TextFactory()) == "[Save] [x]"
assert render_form(SymbolFactory()) == "(Save) ☑"

Work and storage: O(output length) construction.

When to recognize it: Abstract Factory supplies related products together. If there is no family constraint, separate constructors may be enough.

Check before applying it: A new product category affects every family promising compatibility.

Change one rule

Every family now needs a dropdown. Is that just one new factory?

See what changes

No. Extend the family interface and all concrete families. Abstract Factory favors adding families over adding new product categories.

Connect

Choose how an object is built: Extend single-product construction into one compatible product family.

Builder: assemble a valid object: A builder can assemble one complex product from a chosen family.