Abstract Factory 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?

  • One challenge can be designing cars with specific features and configurations for different regions.
  • The other main challenge is to ensure consistency in the production of cars and their specifications within each region.
  • Adapting the system to changes in regulations or introducing new features for a specific region becomes challenging. 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?

  • The abstract factory ensures that each region has its concrete factory, responsible for creating cars and specifications consistent with the local market requirements. This promotes consistency in the design and features of the vehicles produced for each region.
  • Each concrete factory encapsulates the logic for creating cars and specifications specific to a region. This isolation allows you to make changes or introduce new features for a particular region without affecting the rest of the system. For example, if regulations change in North America, you can modify the NorthAmericaCarFactory without impacting the EuropeCarFactor.
  • Adding support for a new region involves creating a new concrete factory for that region. This expansion can be done without modifying existing code, providing a scalable and modular solution.
  • The pattern promotes a clear separation between the creation of products (cars and specifications) and their actual use.

Below is the code of above problem statement using Command Pattern:

1. Abstract Factory Interface (CarFactory)

Defines methods for creating cars and their specifications.

Java




// Abstract Factory Interface
interface CarFactory {
    Car createCar();
    CarSpecification createSpecification();
}


2. Concrete Factories (NorthAmericaCarFactory and EuropeCarFactory)

Implement the abstract factory interface 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)

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();
    }
}


Output

Assembling Sedan car.
North America Car Specification: Safety features compliant with local regulations.
Assembling Hatchback car.
Europe Car Specification: Fuel efficiency and emissions compliant wit...

Abstract Factory Pattern

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, in simpler terms the Abstract Factory Pattern is a way of organizing how you create groups of things that are related to each other.

Important Topics for the Abstract Factory Pattern

  • What is the Abstract Factory Pattern?
  • Components of Abstract Factory Pattern
  • Abstract Factory Pattern example
  • Advantages of using Abstract Factory Pattern
  • Disadvantages of using Abstract Factory Pattern
  • When to use Abstract Factory Pattern
  • When not to use Abstract Factory Pattern

Similar Reads

What is the Abstract Factory Pattern?

The Abstract Factory Pattern is a way of organizing how you create groups of things that are related to each other. It provides a set of rules or instructions that let you create different types of things without knowing exactly what those things are. This helps you keep everything organized and lets you switch between different types easily, following the same set of rules....

Components of Abstract Factory Pattern

1. Abstract Factory...

Abstract Factory 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....

Advantages of using Abstract Factory Pattern

...

Disadvantages of using Abstract Factory Pattern

...

When to use Abstract Factory Pattern

...

When not to use Abstract Factory Pattern

...