Home > Java > javaTutorial > body text

Abstract Factory Pattern

Patricia Arquette
Release: 2024-11-21 04:59:10
Original
105 people have browsed it

What is Abstract Factory Pattern?

Abstract Factory Pattern is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.

When to use it?

Use Abstract factory pattern when you have families of objects where objects in one family are expected to work together, yet still want to decouple concrete products & their factories from client code.

Problem

We are planning to develop a simple theme application that client can choose light or dark UI based on their preference. We prepare three components Button, Menu and Toolbar. Each component has light & dark style appearances.
Obviously we want to make sure all light theme components work together (We don't want to display light button, dark menu and light toolbar at the same time). Additionally we want to remain possibility to add other theme in the future such as "High contrast" theme.

Solution

Abstract Factory Pattern

  1. ThemeApplication
    This is our client. The constructor receives concrete factory as its argument, then createComponents() will create corresponding components. Client only depends on an abstract factory & abstract products.

  2. ThemeFactory
    An interface for all the concrete themes.

  3. Concrete ThemeFactories
    Provides actual implementation for ThemeFactory's methods. These concrete factories create concrete products.

  4. Product interfaces
    Interfaces for components.

  5. Concrete products
    Defines specific behavior.

Structure

Abstract Factory Pattern

Implementation in Java

public interface Button {

    void render();
}
Copy after login
public class DarkButton implements Button{

    @Override
    public void render() {
        System.out.println("Successfully render a dark button");
    }
}
Copy after login
public class LightButton implements Button {

    @Override
    public void render() {
        System.out.println("Successfully render a light button");
    }
}

Copy after login

You can write the code for Menu and Toolbar components in the same way.

public interface ThemeFactory {

    Button createButton();

    Menu createMenu();

    Toolbar createToolbar();
}
Copy after login
public class DarkThemeFactory implements ThemeFactory {

    @Override
    public Button createButton() {
        return new DarkButton();
    }

    @Override
    public Menu createMenu() {
        return new DarkMenu();
    }

    @Override
    public Toolbar createToolbar() {
        return new DarkToolbar();
    }
}
Copy after login
public class LightThemeFactory implements ThemeFactory {

    @Override
    public Button createButton() {
        return new LightButton();
    }

    @Override
    public Menu createMenu() {
        return new LightMenu();
    }

    @Override
    public Toolbar createToolbar() {
        return new LightToolbar();
    }
}
Copy after login
// This is our client code, notice client sees neither concrete products nor concrete factories
public class ThemeApplication {

    private ThemeFactory themeFactory;
    private Button button;
    private Menu menu;
    private Toolbar toolbar;

    public ThemeApplication(ThemeFactory factory) {
        themeFactory = factory;
        createComponents();
    }

    public void createComponents() {
        button = themeFactory.createButton();
        menu = themeFactory.createMenu();
        toolbar = themeFactory.createToolbar();
    }

    public void renderComponents() {
        button.render();
        menu.render();
        toolbar.render();
    }
}
Copy after login
public class ThemeApplicationTestDrive {

    public static void main (String[] args) {
        ThemeFactory darkFactory = new DarkThemeFactory();
        ThemeApplication app1 = new ThemeApplication(darkFactory);
        app1.renderComponents();
        System.out.println("*******************");

        ThemeFactory lightFactory = new LightThemeFactory();
        ThemeApplication app2 = new ThemeApplication(lightFactory);
        app2.renderComponents();
    }
}
Copy after login

Output:

Successfully render a dark button
Successfully render a dark menu
Successfully render a dark toolbar
*******************
Successfully render a light button
Successfully render a light menu
Successfully render a light toolbar
Copy after login

Pitfalls

  • More complex to implement than factory pattern.
  • Adding a new product requires changes in both abstract factory & concrete factories.

Comparison with Factory Pattern

  • Factory pattern decouples concrete products from client code, while Abstract factory pattern hides concrete factories as well as concrete products.
  • Factory pattern works with one product & its subclasses. In contrast, Abstract factory pattern is suitable when multiple products are expected to work together.

You can check all the design pattern implementations here.
GitHub Repository


P.S.
I'm new to write tech blog, if you have advice to improve my writing, or have any confusing point, please leave a comment!
Thank you for reading :)

The above is the detailed content of Abstract Factory Pattern. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template