0Pricing
Java Academy · Lesson

Abstract Factory for Product Families

Create families of related objects (e.g., UI components per platform) with Abstract Factory.

Abstract Factory for Product Families is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Abstract Factory Intent

Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes. It ensures that products from one factory are compatible.

The Problem: Inconsistent Product Families

Without Abstract Factory, you might mix a Windows button with a Mac checkbox. The pattern enforces that all products come from the same factory, ensuring visual or behavioral consistency.

Abstract Factory Interface

Define an interface with a creation method for each product in the family. Each concrete factory implements all methods for one variant.

public interface UIFactory {
    Button   createButton();
    Checkbox createCheckbox();
    TextField createTextField();
}

Concrete Factories

Each concrete factory creates all products for one platform or theme. The client code only references the interface, not the concrete factories.

public class WindowsUIFactory implements UIFactory {
    public Button    createButton()    { return new WindowsButton(); }
    public Checkbox  createCheckbox()  { return new WindowsCheckbox(); }
    public TextField createTextField() { return new WindowsTextField(); }
}
public class MacUIFactory implements UIFactory {
    public Button    createButton()    { return new MacButton(); }
    public Checkbox  createCheckbox()  { return new MacCheckbox(); }
    public TextField createTextField() { return new MacTextField(); }
}

Abstract Product Interfaces

Each product type has its own interface. Concrete products implement it for each platform. The client codes against the abstract product, not the concrete one.

public interface Button   { void render(); void onClick(); }
public interface Checkbox  { void render(); boolean isChecked(); }
public interface TextField { void render(); String getValue(); }

Client Using the Factory

The client receives a factory through its constructor. It calls factory methods to create widgets and uses them through abstract product interfaces — completely decoupled from concrete classes.

public class Application {
    private final UIFactory factory;
    public Application(UIFactory factory) { this.factory = factory; }
    public void buildUI() {
        Button btn = factory.createButton();
        Checkbox cb = factory.createCheckbox();
        btn.render();
        cb.render();
    }
}

Choosing the Factory at Runtime

Select the concrete factory based on environment or configuration and inject it. The rest of the application never mentions platform-specific classes.

String os = System.getProperty("os.name").toLowerCase();
UIFactory factory = os.contains("mac") ? new MacUIFactory() : new WindowsUIFactory();
new Application(factory).buildUI();

Adding a New Product to the Family

Adding a new product (e.g., Slider) requires updating the abstract factory interface and all concrete factories — a drawback of the pattern.

public interface UIFactory {
    Button   createButton();
    Checkbox createCheckbox();
    Slider   createSlider(); // new product — must update ALL factories
}

Abstract Factory vs Factory Method

Factory Method creates one product via subclassing. Abstract Factory creates a product suite via composition. Abstract Factory is typically implemented using a set of Factory Methods.

Abstract Factory for Database Drivers

A ConnectionFactory abstract factory can produce Connection, Statement, and ResultSet objects. Swap between MySQL and PostgreSQL by changing the factory.

public interface DBFactory {
    Connection  createConnection();
    Statement   createStatement();
}
public class MySQLFactory implements DBFactory { ... }
public class PgFactory   implements DBFactory { ... }

Abstract Factory with Spring Profiles

In Spring Boot, define a UIFactory bean differently per profile: the application always injects UIFactory; Spring resolves the correct concrete factory for the active profile.

@Configuration
@Profile("mac")
public class MacConfig {
    @Bean UIFactory uiFactory() { return new MacUIFactory(); }
}

Ensuring Consistency

The core benefit: a factory only produces compatible products. You can never accidentally get a Mac checkbox paired with a Windows button when using Abstract Factory properly.

Quick Check

What does Abstract Factory guarantee that Factory Method does not?

Recap

Abstract Factory creates families of related objects through a common interface. Swap entire product families by changing the factory. Best when products must be consistent; adds overhead when adding new product types.

Frequently asked questions

Is the “Abstract Factory for Product Families” lesson free?

Yes — the full text of “Abstract Factory for Product Families” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Abstract Factory for Product Families”?

Create families of related objects (e.g., UI components per platform) with Abstract Factory. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Abstract Factory for Product Families” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Singleton: Thread-Safe Implementations
  2. Factory Method Pattern
  3. Abstract Factory for Product Families
  4. Builder Pattern with Fluent API
← Back to Java Academy