The Abstract Factory Design Pattern is a creational pattern that provides an interface to create families of related or dependent objects without specifying their concrete classes. This pattern is particularly useful when a system needs to be independent of how its objects are created.
Here's an example implementation of the Abstract Factory Pattern in Java:
Step 1: Define Abstract Product Interfaces
interface Button {
void paint();
}
interface Checkbox {
void paint();
}
Step 2: Create Concrete Product Classes
class WindowsButton implements Button {
@Override
public void paint() {
System.out.println("Rendering a button in a Windows style");
}
}
class MacOSButton implements Button {
@Override
public void paint() {
System.out.println("Rendering a button in a macOS style");
}
}
class WindowsCheckbox implements Checkbox {
@Override
public void paint() {
System.out.println("Rendering a checkbox in a Windows style");
}
}
class MacOSCheckbox implements Checkbox {
@Override
public void paint() {
System.out.println("Rendering a checkbox in a macOS style");
}
}
Step 3: Define an Abstract Factory Interface
interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
Step 4: Create Concrete Factory Classes
class WindowsFactory implements GUIFactory {
@Override
public Button createButton() {
return new WindowsButton();
}
@Override
public Checkbox createCheckbox() {
return new WindowsCheckbox();
}
}
class MacOSFactory implements GUIFactory {
@Override
public Button createButton() {
return new MacOSButton();
}
@Override
public Checkbox createCheckbox() {
return new MacOSCheckbox();
}
}
Step 5: Use the Factory in the Client Code
class Application {
private Button button;
private Checkbox checkbox;
public Application(GUIFactory factory) {
button = factory.createButton();
checkbox = factory.createCheckbox();
}
public void paint() {
button.paint();
checkbox.paint();
}
}
class Demo {
public static Application configureApplication() {
Application app;
GUIFactory factory;
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("mac")) {
factory = new MacOSFactory();
} else {
factory = new WindowsFactory();
}
app = new Application(factory);
return app;
}
public static void main(String[] args) {
Application app = configureApplication();
app.paint();
}
}
Explanation
- Abstract Products: Define interfaces for products (e.g.,
Button, Checkbox).
- Concrete Products: Implement the products for different operating systems (e.g.,
WindowsButton, MacOSButton).
- Abstract Factory: An interface (
GUIFactory) declares the creation methods for products.
- Concrete Factories: Implemented classes that return product instances (
WindowsFactory, MacOSFactory).
- Client: Uses the factory to create products. In this case, the application (
Demo class) is the client, which configures the application's products based on the operating system.
This pattern ensures that the client code can work with different product families without being tightly coupled to specific classes.