Abstract Factory Pattern is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.
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.
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.
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.
ThemeFactory
An interface for all the concrete themes.
Concrete ThemeFactories
Provides actual implementation for ThemeFactory's methods. These concrete factories create concrete products.
Product interfaces
Interfaces for components.
Concrete products
Defines specific behavior.
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
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!