Abstract Factory Design Pattern: Creating Families of Related Objects

The Abstract Factory Design Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It is an extension of the Factory Pattern and is useful when you need to create groups of objects that work together.
Why Use the Abstract Factory Pattern?
Consistency: Ensures that the created objects are compatible and work together.
Decoupling: Separates object creation logic from the main logic.
Flexibility: Makes it easy to switch between different families of objects.
Example: Creating UI Components for Different Operating Systems
Let’s say you’re building a cross-platform application that needs to create UI components (e.g., buttons, checkboxes) for different operating systems (e.g., Windows, macOS). The Abstract Factory Pattern can help you create families of related UI components for each OS.
Step-by-Step Implementation
Step 1: Create Abstract Product Interfaces
Define interfaces for each type of UI component.
// Button interface
public interface Button {
void render();
}
// Checkbox interface
public interface Checkbox {
void render();
}
Step 2: Create Concrete Product Classes
Implement the interfaces for each operating system.
// Windows Button
public class WindowsButton implements Button {
@Override
public void render() {
System.out.println("Rendering a Windows button.");
}
}
// Windows Checkbox
public class WindowsCheckbox implements Checkbox {
@Override
public void render() {
System.out.println("Rendering a Windows checkbox.");
}
}
// macOS Button
public class MacOSButton implements Button {
@Override
public void render() {
System.out.println("Rendering a macOS button.");
}
}
// macOS Checkbox
public class MacOSCheckbox implements Checkbox {
@Override
public void render() {
System.out.println("Rendering a macOS checkbox.");
}
}
Step 3: Create the Abstract Factory Interface
Define an interface for creating families of related UI components.
public interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}
Step 4: Create Concrete Factory Classes
Implement the GUIFactory
interface for each operating system.
// Windows Factory
public class WindowsFactory implements GUIFactory {
@Override
public Button createButton() {
return new WindowsButton();
}
@Override
public Checkbox createCheckbox() {
return new WindowsCheckbox();
}
}
// macOS Factory
public class MacOSFactory implements GUIFactory {
@Override
public Button createButton() {
return new MacOSButton();
}
@Override
public Checkbox createCheckbox() {
return new MacOSCheckbox();
}
}
Step 5: Use the Abstract Factory to Create UI Components
Now, you can use the abstract factory to create families of related UI components for a specific operating system.
public class Main {
public static void main(String[] args) {
// Create a Windows UI
GUIFactory windowsFactory = new WindowsFactory();
Button windowsButton = windowsFactory.createButton();
Checkbox windowsCheckbox = windowsFactory.createCheckbox();
windowsButton.render();
windowsCheckbox.render();
// Create a macOS UI
GUIFactory macOSFactory = new MacOSFactory();
Button macOSButton = macOSFactory.createButton();
Checkbox macOSCheckbox = macOSFactory.createCheckbox();
macOSButton.render();
macOSCheckbox.render();
}
}
Output
Rendering a Windows button.
Rendering a Windows checkbox.
Rendering a macOS button.
Rendering a macOS checkbox.
Key Points About the Abstract Factory Pattern
Families of Objects: Creates groups of related objects that work together.
Decoupling: Hides the creation logic from the client.
Extensibility: Easy to add new families of objects without modifying existing code.
Real-Life Analogy
Think of the Abstract Factory Pattern like a furniture store. If you buy a chair from a modern furniture set, you’ll also want a table from the same set to ensure consistency. Similarly, the Abstract Factory Pattern ensures that the created objects are compatible and belong to the same family.
When to Use the Abstract Factory Pattern?
When you need to create families of related objects.
When you want to ensure that the created objects are compatible.
When you need to support multiple variants of a product family.
Subscribe to my newsletter
Read articles from Niharika Maruvada directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Niharika Maruvada
Niharika Maruvada
JAVA Developer Passionate about crafting innovative and efficient solutions in the FinTech industry, I thrive on building cutting-edge applications that solve real-world problems. With a strong focus on clean, scalable, and maintainable code, I aim to drive business success through technology. Always eager to embrace new challenges and expand my skill set, I am committed to staying at the forefront of emerging technologies and best practices. My dedication to continuous learning and professional growth fuels my ability to deliver impactful results. Let’s connect and build the future of FinTech together! #Java #FinTech #Innovation #CleanCode #TechEnthusiast #ContinuousLearning