Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • System Design Tutorial
  • What is System Design
  • System Design Life Cycle
  • High Level Design HLD
  • Low Level Design LLD
  • Design Patterns
  • UML Diagrams
  • System Design Interview Guide
  • Scalability
  • Databases
Open In App
Next Article:
Singleton Method Design Pattern in JavaScript
Next article icon

Abstract Factory Pattern

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Abstract Factory Pattern is one of the creational design patterns that provides an interface for creating families of related or dependent objects without specifying their concrete classes and implementation, in simpler terms the Abstract Factory Pattern is a way of organizing how you create groups of things that are related to each other.

Abstract-factory-pattern

Table of Content

  • What is the Abstract Factory Pattern?
  • Components of Abstract Factory Pattern
  • Abstract Factory 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

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.

  • 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.

Components of Abstract Factory Pattern

To understand abstract factory pattern, we have to understand the components of it and relationships between them.

  • Abstract Factory:
    • Abstract Factory provides a high-level blueprint that defines rules for creating families of related object without specifying their concrete classes.
    • 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.
    • Also multiple concrete factories can exist, each produce a distinct family of related objects.
  • Abstract Products:
    • Abstract Products represents a family of related objects by defining a set of common methods or properties.
    • 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 are the actual instances of objects created by concrete factories.
    • 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.

AbstractFactoryexampledrawio-(1)-2

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

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

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.

Next Article
Singleton Method Design Pattern in JavaScript

S

Saket Kumar
Improve
Article Tags :
  • Design Pattern
  • System Design

Similar Reads

    Software Design Patterns Tutorial
    Software design patterns are important tools developers, providing proven solutions to common problems encountered during software development. This article will act as tutorial to help you understand the concept of design patterns. Developers can create more robust, maintainable, and scalable softw
    9 min read
    Complete Guide to Design Patterns
    Design patterns help in addressing the recurring issues in software design and provide a shared vocabulary for developers to communicate and collaborate effectively. They have been documented and refined over time by experienced developers and software architects. Important Topics for Guide to Desig
    11 min read
    Types of Software Design Patterns
    Designing object-oriented software is hard, and designing reusable object-oriented software is even harder. Christopher Alexander says, "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way th
    9 min read

    1. Creational Design Patterns

    Creational Design Patterns
    Creational Design Patterns focus on the process of object creation or problems related to object creation. They help in making a system independent of how its objects are created, composed, and represented. Creational patterns give a lot of flexibility in what gets created, who creates it, and how i
    4 min read

    Types of Creational Patterns

    Factory method Design Pattern
    The Factory Method Design Pattern is a creational design pattern that provides an interface for creating objects in a superclass, allowing subclasses to alter the type of objects that will be created. This pattern is particularly useful when the exact types of objects to be created may vary or need
    7 min read
    Abstract Factory Pattern
    The Abstract Factory Pattern is one of the creational design patterns that provides an interface for creating families of related or dependent objects without specifying their concrete classes and implementation, in simpler terms the Abstract Factory Pattern is a way of organizing how you create gro
    8 min read
    Singleton Method Design Pattern in JavaScript
    Singleton Method or Singleton Design Pattern is a part of the Gang of Four design pattern and it is categorized under creational design patterns. It is one of the most simple design patterns in terms of modeling but on the other hand, this is one of the most controversial patterns in terms of comple
    9 min read
    Singleton Method Design Pattern
    The Singleton Method Design Pattern ensures a class has only one instance and provides a global access point to it. It’s ideal for scenarios requiring centralized control, like managing database connections or configuration settings. This article explores its principles, benefits, drawbacks, and bes
    11 min read
    Prototype Design Pattern
    The Prototype Design Pattern is a creational pattern that enables the creation of new objects by copying an existing object. Prototype allows us to hide the complexity of making new instances from the client. The existing object acts as a prototype and contains the state of the object. Table of Cont
    8 min read
    Builder Design Pattern
    The Builder Design Pattern is a creational pattern used in software design to construct a complex object step by step. It allows the construction of a product in a step-by-step manner, where the construction process can change based on the type of product being built. This pattern separates the cons
    9 min read

    2. Structural Design Patterns

    Structural Design Patterns
    Structural Design Patterns are solutions in software design that focus on how classes and objects are organized to form larger, functional structures. These patterns help developers simplify relationships between objects, making code more efficient, flexible, and easy to maintain. By using structura
    7 min read

    Types of Structural Patterns

    Adapter Design Pattern
    One structural design pattern that enables the usage of an existing class's interface as an additional interface is the adapter design pattern. To make two incompatible interfaces function together, it serves as a bridge. This pattern involves a single class, the adapter, responsible for joining fun
    8 min read
    Bridge Design Pattern
    The Bridge design pattern allows you to separate the abstraction from the implementation. It is a structural design pattern. There are 2 parts in Bridge design pattern : AbstractionImplementationThis is a design mechanism that encapsulates an implementation class inside of an interface class. The br
    4 min read
    Composite Method | Software Design Pattern
    Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. The main idea behind the Composite Pattern is to build a tree structure of objects, where individual objects and composite objects share a common interface. T
    9 min read
    Decorator Design Pattern
    The Decorator Design Pattern is a structural design pattern that allows behavior to be added to individual objects dynamically, without affecting the behavior of other objects from the same class. It involves creating a set of decorator classes that are used to wrap concrete components.Important Top
    9 min read
    Facade Method Design Pattern
    Facade Method Design Pattern is a part of the Gang of Four design patterns and it is categorized under Structural design patterns. Before we go into the details, visualize a structure. The house is the facade, it is visible to the outside world, but beneath it is a working system of pipes, cables, a
    8 min read
    Flyweight Design Pattern
    The Flyweight design pattern is a structural pattern that optimizes memory usage by sharing a common state among multiple objects. It aims to reduce the number of objects created and to decrease memory footprint, which is particularly useful when dealing with a large number of similar objects.Flywei
    10 min read
    Proxy Design Pattern
    The Proxy Design Pattern a structural design pattern is a way to use a placeholder object to control access to another object. Instead of interacting directly with the main object, the client talks to the proxy, which then manages the interaction. This is useful for things like controlling access, d
    9 min read

    3. Behvioural Design Patterns

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences