The Abstract Factory Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It acts as a "factory of factories," where a super-factory creates other factories that in turn produce specific objects. This adds a higher level of abstraction, allowing systems to switch between different product families easily while keeping the code organized and loosely coupled.
- Works around a super-factory that produces multiple factories.
- Concrete factories created at runtime decide the actual product types.
- Useful when the system needs to be independent of how products are created or represented.
- Makes it easy to switch between different groups of related objects without code changes.
Real Life Software Examples
- I applications that need to support multiple cloud platforms (e.g., AWS, Azure, Google Cloud), the Abstract Factory Pattern facilitates the creation of cloud-specific services. An abstract factory can define interfaces for services like storage, compute, and networking.
- In applications that need to support multiple database systems (e.g., SQL, NoSQL), the Abstract Factory Pattern can be used to create database connections and queries. An abstract factory defines interfaces for creating database-related objects.
Components of Abstract Factory Pattern
To understand abstract factory pattern, we have to understand the components of it and relationships between them.
- Abstract Factory: It provides a way such that concrete factories follow a common interface, providing consistent way to produce related set of objects.
- Concrete Factories: Concrete Factories implement the rules specified by the abstract factory. It contain the logic for creating specific instances of objects within a family.
- Abstract Products: It acts as an abstract or interface type that all concrete products within a family must follow to and provides a unified way for concrete products to be used interchangeably.
- Concrete Products: They implement the methods declared in the abstract products, ensuring consistency within a family and belong to a specific category or family of related objects.
- Client: Client utilizes the abstract factory to create families of objects without specifying their concrete types and interacts with objects through abstract interfaces provided by abstract products.
Example of Abstract Factory Design Pattern
Let's understand Abstract Factory Design Pattern using an example:
Imagine you're managing a global car manufacturing company
- You want to design a system to create cars with specific configurations for different regions, such as North America and Europe.
- Each region may have unique requirements and regulations, and you want to ensure that cars produced for each region meet those standards.
What can be the challenges while implementing this system?
- Different regions have different cars with different features, so designing this can be challenging.
- The other main challenge is to ensure consistency in the production of cars and their specifications within each region.
- There can be updation in having new cars in different regions so adapting the system to changes in regulations or introducing new features for a specific region becomes challenging.
- So, Modifications would need to be made in multiple places, increasing the chances of introducing bugs and making the system more prone to errors.
How Abstracy Factory Pattern help to solve above challenges?
Below is how abstract factory pattern help to solve the above challenges. After using this pattern:
- Different regions has their own factory to create cars for local needs.
- This helps to keeps the design and features the same for vehicles in each region.
- You can change one region without affecting others (e.g., updating North America doesn’t impact Europe).
- To add a new region, just create a new factory, no need to change existing code.
- The pattern keeps car creation separate from how they are used.
-2.png)
Below is the code of above problem statement using Abstract Factory Pattern:
1. Abstract Factory Interface (CarFactory)
"CarFactory" is a Abstract Factory Interface that defines methods for creating cars and their specifications.
Java
// Abstract Factory Interface
interface CarFactory {
Car createCar();
CarSpecification createSpecification();
}
2. Concrete Factories (NorthAmericaCarFactory and EuropeCarFactory)
"NorthAmericaCarFactory" and "EuropeCarFactory" are concrete factories that implement the abstract factory interface "CarFactory" to create cars and specifications specific to North America, Europe.
Java
// Concrete Factory for North America Cars
class NorthAmericaCarFactory implements CarFactory {
public Car createCar() {
return new Sedan();
}
public CarSpecification createSpecification() {
return new NorthAmericaSpecification();
}
}
// Concrete Factory for Europe Cars
class EuropeCarFactory implements CarFactory {
public Car createCar() {
return new Hatchback();
}
public CarSpecification createSpecification() {
return new EuropeSpecification();
}
}
}
3. Abstract Products (Car and CarSpecification interfaces)
Define interfaces for cars and specifications to ensure a common structure.
Java
// Abstract Product Interface for Cars
interface Car {
void assemble();
}
// Abstract Product Interface for Car Specifications
interface CarSpecification {
void display();
}
4. Concrete Products (Sedan, Hatchback, NorthAmericaSpecification, EuropeSpecification)
"Sedan", "Hatchback", "NorthAmericaSpecification", "EuropeSpecification" are concrete products that implement the interfaces to create specific instances of cars and specifications.
Java
// Concrete Product for Sedan Car
class Sedan implements Car {
public void assemble() {
System.out.println("Assembling Sedan car.");
}
}
// Concrete Product for Hatchback Car
class Hatchback implements Car {
public void assemble() {
System.out.println("Assembling Hatchback car.");
}
}
// Concrete Product for North America Car Specification
class NorthAmericaSpecification implements CarSpecification {
public void display() {
System.out.println("North America Car Specification: Safety features compliant with local regulations.");
}
}
// Concrete Product for Europe Car Specification
class EuropeSpecification implements CarSpecification {
public void display() {
System.out.println("Europe Car Specification: Fuel efficiency and emissions compliant with EU standards.");
}
}
Complete code for the above example
Below is the complete code for the above example:
Java
// Abstract Factory Interface
interface CarFactory {
Car createCar();
CarSpecification createSpecification();
}
// Concrete Factory for North America Cars
class NorthAmericaCarFactory implements CarFactory {
public Car createCar() {
return new Sedan();
}
public CarSpecification createSpecification() {
return new NorthAmericaSpecification();
}
}
// Concrete Factory for Europe Cars
class EuropeCarFactory implements CarFactory {
public Car createCar() {
return new Hatchback();
}
public CarSpecification createSpecification() {
return new EuropeSpecification();
}
}
// Abstract Product Interface for Cars
interface Car {
void assemble();
}
// Abstract Product Interface for Car Specifications
interface CarSpecification {
void display();
}
// Concrete Product for Sedan Car
class Sedan implements Car {
public void assemble() {
System.out.println("Assembling Sedan car.");
}
}
// Concrete Product for Hatchback Car
class Hatchback implements Car {
public void assemble() {
System.out.println("Assembling Hatchback car.");
}
}
// Concrete Product for North America Car Specification
class NorthAmericaSpecification implements CarSpecification {
public void display() {
System.out.println("North America Car Specification: Safety features compliant with local regulations.");
}
}
// Concrete Product for Europe Car Specification
class EuropeSpecification implements CarSpecification {
public void display() {
System.out.println("Europe Car Specification: Fuel efficiency and emissions compliant with EU standards.");
}
}
// Client Code
public class CarFactoryClient {
public static void main(String[] args) {
// Creating cars for North America
CarFactory northAmericaFactory = new NorthAmericaCarFactory();
Car northAmericaCar = northAmericaFactory.createCar();
CarSpecification northAmericaSpec = northAmericaFactory.createSpecification();
northAmericaCar.assemble();
northAmericaSpec.display();
// Creating cars for Europe
CarFactory europeFactory = new EuropeCarFactory();
Car europeCar = europeFactory.createCar();
CarSpecification europeSpec = europeFactory.createSpecification();
europeCar.assemble();
europeSpec.display();
}
}
OutputAssembling Sedan car.
North America Car Specification: Safety features compliant with local regulations.
Assembling Hatchback car.
Europe Car Specification: Fuel efficiency and emissions compliant wit...
Benefits of using Abstract Factory Pattern
Below are the main benefits of abstract factory pattern:
- The Abstract Factory pattern separates the creation of objects, so clients don’t need to know specific classes.
- Clients interact with objects through abstract interfaces, keeping class names hidden from client code.
- Changing the factory allows for different product configurations, as all related products change together.
- The pattern ensures that an application uses objects from only one family at a time for better compatibility.
Challenges of using Abstract Factory Pattern
Below are the main challenges of using abstract factory pattern:
- The Abstract Factory pattern can add unnecessary complexity to simpler projects with multiple factories and interfaces.
- Adding new product types may require changes to both concrete factories and the abstract factory interface, impacting existing code.
- Introducing more factories and product families can quickly increase the number of classes, making code management difficult in smaller projects.
- It may violate the Dependency Inversion Principle if client code depends directly on concrete factories rather than abstract interfaces.
When to use Abstract Factory Pattern
Choose using abstract factory pattern when:
- When your system requires multiple families of related products and you want to ensure compatibility between them.
- When you need flexibility and extensibility, allowing for new product variants to be added without changing existing client code.
- When you want to encapsulate the creation logic, making it easier to modify or extend the object creation process without affecting the client.
- When you aim to maintain consistency across different product families, ensuring a uniform interface for the products.
When not to use Abstract Factory Pattern
Aviod using abstract factory pattern when:
- The product families are unlikely to change, as it may add unnecessary complexity.
- When your application only requires single, independent objects and isn't concerned with families of related products.
- When overhead of maintaining multiple factories outweighs the benefits, particularly in smaller applications.
- When simpler solutions, like the Factory Method or Builder pattern, if they meet your needs without adding the complexity of the Abstract Factory pattern.
Explore
What is System Design
System Design Fundamentals
Scalability in System Design
Databases in Designing Systems
High Level Design(HLD)
Low Level Design(LLD)
Design Patterns
Interview Guide for System Design
System Design Interview Questions & Answers