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
ThemeApplication
This is our client. The constructor receives concrete factory as its argument, thencreateComponents()
will create corresponding components. Client only depends on an abstract factory & abstract products.ThemeFactory
An interface for all the concrete themes.Concrete ThemeFactories
Provides actual implementation forThemeFactory
's methods. These concrete factories create concrete products.Product interfaces
Interfaces for components.Concrete products
Defines specific behavior.
Structure
Implementation in Java
public interface Button {
void render();
}
public class DarkButton implements Button{
@Override
public void render() {
System.out.println("Successfully render a dark button");
}
}
public class LightButton implements Button {
@Override
public void render() {
System.out.println("Successfully render a light button");
}
}
You can write the code for Menu
and Toolbar
components in the same way.
public interface ThemeFactory {
Button createButton();
Menu createMenu();
Toolbar createToolbar();
}
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();
}
}
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();
}
}
// 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();
}
}
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();
}
}
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
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 :)
Top comments (0)