0% found this document useful (0 votes)
3 views5 pages

Abstract Factory Patternpdf

The Abstract Factory Pattern provides an interface for creating families of related objects without specifying their concrete classes, enabling clients to switch between product families at runtime. It involves defining abstract product interfaces, concrete products, and factories that produce these products, exemplified through a car manufacturing scenario. The pattern is particularly useful for ensuring related products work well together and adheres to the Open/Closed Principle.

Uploaded by

Mohit Baid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Abstract Factory Patternpdf

The Abstract Factory Pattern provides an interface for creating families of related objects without specifying their concrete classes, enabling clients to switch between product families at runtime. It involves defining abstract product interfaces, concrete products, and factories that produce these products, exemplified through a car manufacturing scenario. The pattern is particularly useful for ensuring related products work well together and adheres to the Open/Closed Principle.

Uploaded by

Mohit Baid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Abstract Factory

Definition
Class Diagram
Example: Car Manufacturing
Implementation
Factory Method vs Abstract Factory

Resources

Video → 27. All Creational Design Patterns | Prototype, Singleton, Fac


tory, AbstractFactory, Builder Pattern
Video → 5. Factory Pattern Vs Abstract Factory Pattern Explanation
(Hindi) | Low Level System Design

Definition
The Abstract Factory Pattern provides an interface for creating families of related objects
without specifying their concrete classes. It's like having multiple factories, where each
factory produces a complete family of products that work together/are related/dependent.
As a result, an Abstract Factory is known as the Factory of Factories or a Super Factory. It is more complex than
the Factory pattern, as it is responsible for creating families of multiple related product types. Hence, we see
multiple factory methods in the interface. This high level of abstraction enables the client to switch between
different product families at runtime without knowing the concrete implementation of product instantiation.

Class Diagram

Example: Car Manufacturing


1. Abstract Product Interfaces( CarInterior , CarExterior )
Define interfaces for a family of related products.
Each product type has its interface.
Clients depend on this interface to create objects without needing to know their specific types.
2. Concrete Products ( EconomyCarInterior , LuxuryCarExterior , etc.)
Implement the abstract product interfaces.
Products from the same family are designed to work together.
3. Abstract Factory ( CarFactory )
Declares creation methods for each product type.
Defines the contract for creating product families.
4. Concrete Factories ( EconomyCarFactory , LuxuryCarFactory )
Implement the abstract factory interface.
Each factory produces products from a single specific family.
5. Factory Provider
Returns a concrete factory based on client input in the request.
This concrete factory is used to create families of concrete products as per the client’s request.
6. Client ( Application )
Utilizes the Abstract Factory Interface to request concrete products.

Implementation

1. Abstract Product Interfaces

1 // Step 1: Abstract Product interfaces - Define product families


2 public interface CarExterior {
3 void addExteriorComponents();
4 }
5 public interface CarInterior {
6 void addInteriorComponents();
7 }

2. Concrete Products

1 // Step 2: Concrete Products for Economy Car Family


2 public class EconomyCarExterior implements CarExterior {
3 @Override
4 public void addExteriorComponents() {
5 [Link]("Adding basic exterior components for
Economy Car.");
6 }
7 }
8 public class EconomyCarInterior implements CarInterior {
9 @Override
10 public void addInteriorComponents() {
11 [Link]("Adding basic interior components for
Economy Car.");
12 }
13 }
14 // Step 3: Concrete Products for Luxury Car Family
15 public class LuxuryCarExterior implements CarExterior {
16 @Override
17 public void addExteriorComponents() {
18 [Link]("Adding luxurious exterior components for
Luxury Car.");
19 }
20 }
21 public class LuxuryCarInterior implements CarInterior {
22 @Override
23 public void addInteriorComponents() {
24 [Link]("Adding luxurious interior components for
Luxury Car.");
25 }
26 }

3. Abstract Factory

1 // Step 4: Abstract Factory interface


2 public interface CarFactory {
3
4 // Factory methods
5 CarInterior createInterior();
6
7 CarExterior createExterior();
8
9 // Template method that uses all factory methods
10 default void produceCompleteVehicle() {
11
12 [Link]("Starting complete vehicle production...");
13
14 // Create all components
15 CarInterior interior = createInterior();
16 CarExterior exterior = createExterior();
17
18 // Assemble the vehicle
19 [Link]();
20 [Link]();
21
22 [Link]("Vehicle production completed!");
23 }
24 }

4. Concrete Factories

1 // Step 5: Concrete Factories


2 public class EconomyCarFactory implements CarFactory {
3 private String brand;
4
5 public EconomyCarFactory(String brand) {
6 [Link] = brand;
7 }
8 @Override
9 public CarInterior createInterior() {
10 return new EconomyCarInterior();
11 }
12
13 @Override
14 public CarExterior createExterior() {
15 return new EconomyCarExterior();
16 }
17 }
18 // Step 5: Concrete Factories
19 public class LuxuryCarFactory implements CarFactory {
20 private final String brand;
21
22 public LuxuryCarFactory(String brand) {
23 [Link] = brand;
24 }
25
26 @Override
27 public CarInterior createInterior() {
28 return new LuxuryCarInterior();
29 }
30
31 @Override
32 public CarExterior createExterior() {
33 return new LuxuryCarExterior();
34 }
35 }

5. Factory Provider

1 // Step 6: Factory Provider


2 public class CarFactoryProvider {
3 public CarFactory getFactory(CarType type, String brand) {
4 switch (type) {
5 case ECONOMY:
6 return new EconomyCarFactory(brand);
7 case PREMIUM:
8 case LUXURY:
9 return new LuxuryCarFactory(brand);
10 default:
11 throw new IllegalArgumentException("Unknown car type:
" + type);
12 }
13 }
14 }

6. Client

1 // Step 7: Client Application


2 public class AbstractFactoryDemo {
3 public static void main(String[] args) {
4 [Link]("=====Abstract Factory Design
Pattern=====");
5 // Get Factory Provider
6 CarFactoryProvider carFactoryProvider = new
CarFactoryProvider();
7
8 // Get Economy Car Factory
9 CarFactory economyCar =
[Link]([Link], "Honda");
10 [Link]();
11
12 // Get Luxury Car Factory
13 CarFactory luxuryCar =
[Link]([Link], "Mercedes");
14 [Link]();
15
16 // Get Premium Car Factory
17 CarFactory premiumCar =
[Link]([Link], "Rolls Royce");
18 [Link]();
19 }
20 }

Output

The Abstract Factory Pattern is highly effective when you need to ensure that related products work well together,
making it especially suitable for complex systems with various product variants. This highlights the pattern's strong
extensibility and its compliance with the Open/Closed Principle.

Factory Method vs Abstract Factory


Refer → 5. Factory Pattern Vs Abstract Factory Pattern Explanation (Hindi) | Low Level System Design
Summary:
One product, many variants → Use Factory Method
Many products, grouped by family (theme, platform, brand, etc.) → Use Abstract Factory

You might also like