---
title: "Factory vs Abstract Factory Design Patterns in Java"  
description: "Factory and Abstract Factory are two important design patterns used to create objects in java."  
author: "Ashutosh Patel"  
published: 2024-07-22  
updated: 2024-07-22  
canonical: https://www.mindstick.com/articles/336478/factory-vs-abstract-factory-design-patterns-in-java  
category: "java"  
tags: ["java", "design patterns", "programming language"]  
reading_time: 4 minutes  

---

# Factory vs Abstract Factory Design Patterns in Java

Factories and Abstract Factories are two of the most important design structures. Let’s take a closer look at each one,

#### Factory Pattern

The [Factory Pattern](https://www.mindstick.com/blog/10913/java-factory-pattern) is a creation model that provides an interface for creating objects in a superclass, but allows subclasses to change the type of object to be created This encourages loose coupling by detaching the object creation process from client code.

## Key Components

**Factory Interface/Class-** Defines the method. This can be an interface or an [abstract class](https://www.mindstick.com/blog/665/constructor-in-abstract-class-in-c-sharp).

**Concrete Factories-** Use a factory interface to create a specific product.

**Product-** [Manufacturing](https://www.mindstick.com/articles/167329/improving-productivity-within-the-manufacturing-industry) facilities. These can be concrete classes or interfaces.

## Example-

```java
// Product interface
interface Shape {
   void draw();
}
// Concrete products
class Circle implements Shape {
   @Override
   public void draw() {
       System.out.println("Inside Circle::draw() method.");
   }
}
class Rectangle implements Shape {
   @Override
   public void draw() {
       System.out.println("Inside Rectangle::draw() method.");
   }
}
// Factory interface
interface ShapeFactory {
   Shape createShape();
}
// Concrete factories
class CircleFactory implements ShapeFactory {
   @Override
   public Shape createShape() {
       return new Circle();
   }
}
class RectangleFactory implements ShapeFactory {
   @Override
   public Shape createShape() {
       return new Rectangle();
   }
}
// Client code
public class FactoryPatternExample {
   public static void main(String[] args) {
       ShapeFactory circleFactory = new CircleFactory();
       Shape circle = circleFactory.createShape();
       circle.draw();

       ShapeFactory rectangleFactory = new RectangleFactory();
       Shape rectangle = rectangleFactory.createShape();
       rectangle.draw();
   }
}
```

#### Abstract Factory Pattern

\
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects by specifying their concrete classes. Used when there are multiple components to be manufactured and you want to ensure [compatibility](https://www.mindstick.com/blog/303291/best-practices-for-working-with-host-apis-security-compatibility-and-performance-considerations) of components.

## Key Components

\
**Abstract Factory-** Exposes abstract processes.

**Concrete Factories-** Implement an abstract factory interface for the production of concrete objects.

**Abstract Products-** Declares interfaces for related products.

**Concrete products-** Use an abstract product interface to define [specific objects](https://www.mindstick.com/interview/22777/which-app-specific-objects-store-the-app-s-content).

## Example

```java
// Abstract product interfaces
interface Shape {
   void draw();
}
interface Color {
   void fill();
}
// Concrete product classes
class Circle implements Shape {
   @Override
   public void draw() {
       System.out.println("Inside Circle::draw() method.");
   }
}
class Red implements Color {
   @Override
   public void fill() {
       System.out.println("Inside Red::fill() method.");
   }
}
// Abstract factory interface
interface AbstractFactory {
   Shape createShape();
   Color createColor();
}
// Concrete factory implementations
class ShapeFactory implements AbstractFactory {
   @Override
   public Shape createShape() {
       return new Circle();
   }

   @Override
   public Color createColor() {
       // Not used in this example
       return null;
   }
}
class ColorFactory implements AbstractFactory {
   @Override
   public Shape createShape() {
       // Not used in this example
       return null;
   }

   @Override
   public Color createColor() {
       return new Red();
   }
}
// Client code
public class AbstractFactoryPatternExample {
   public static void main(String[] args) {
       AbstractFactory shapeFactory = new ShapeFactory();
       Shape circle = shapeFactory.createShape();
       circle.draw();

       AbstractFactory colorFactory = new ColorFactory();
       Color red = colorFactory.createColor();
       red.fill();
   }
}
```

#### Key Differences

## Intent

\
**Factory Pattern-** Creates objects based on a [generic interface](https://www.mindstick.com/forum/160400/how-to-implement-a-generic-interface-in-c-sharp-provide-an-example), so that client code can manipulate these objects through the interface.

**Abstract Factory Model-** Provides an interface for creating families of related or dependent objects, ensuring product consistency.

**Uses**\

**Factory Pattern-** Used when [responsibility](https://answers.mindstick.com/qa/104498/what-is-the-significance-of-the-fiscal-responsibility-and-budget-management-act) for manufacturing has to be transferred to another factory, especially useful when manufacturing is complex or when there are multiple [production processes](https://answers.mindstick.com/qa/112836/how-are-zero-waste-initiatives-transforming-consumer-habits-and-production-processes).

**Abstract Factory Pattern-** It used when there are multiple families of related objects or when the system needs flexibility in how it creates, organizes and represents its objects.

\
Both models simplify the creation process but satisfy conditions. Factory patterns are about creating individual objects, while abstract factory patterns are a family of related objects. Each model supports different levels of [abstraction](https://www.mindstick.com/blog/668/abstraction-and-encapsulation) and flexibility in object creation in Java applications.

**Also, Read:** [Explain the SOLID principle in Java programming](https://www.mindstick.com/articles/336475/explain-the-solid-principle-in-java-programming)

---

Original Source: https://www.mindstick.com/articles/336478/factory-vs-abstract-factory-design-patterns-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
