Abstract Factory
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.
- Abstract Factory pattern is almost same as Factory Pattern and is considered as another layer of abstraction over factory pattern.
- Abstract Factory patterns work around a super-factory which creates other factories.
- At runtime, the abstract factory is coupled with any desired concrete factory which can create objects of the desired type.
Example of Abstract Factory Design Pattern
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 kees 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.
* Key Note "Your giving an access by returning the factory interface to your consumer/client "
// 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();
}
}