DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Builder Design Pattern in Java
  • Abstract Factory Pattern In Java
  • Understanding the Circuit Breaker: A Key Design Pattern for Resilient Systems
  • Designing Scalable Multi-Agent AI Systems: Leveraging Domain-Driven Design and Event Storming

Trending

  • Designing Configuration-Driven Apache Spark SQL ETL Jobs with Delta Lake CDC
  • Optimizing Your IDP Using Centralized Configuration Management With IBM Cloud App Configuration: A Complete Guide
  • Advancing DSLs in the Age of GenAI
  • Real-Time Webcam-Based Sign Language and Speech Bidirectional Translation System

Intro to Design Patterns: Abstract Factory Pattern

By 
Andre Mare user avatar
Andre Mare
·
Apr. 30, 08 · News
Likes (0)
Comment
Save
Tweet
Share
28.6K Views

Join the DZone community and get the full member experience.

Join For Free

Andre Mare's series on "Gang of Four" design patterns continues. First, he covered the Builder Pattern, next the Factory Method Pattern. Today he continues with the "Abstract Factory Pattern". Read on for all the details, diagrams, and sample code! -- Geertjan Wielenga, JavaLobby Zone Leader

 

Intent

Provide an interface for creating families of related or dependent objects without specifying their concrete classes. - Gof

Type

Object Creational

Solution

The Abstract Factory Pattern defines an interface with a set of methods to create a family of related products. The family of related objects is defined through a set of product types. The implementation of the product types is delegated to a set of concrete product subclasses. The creation of the concrete product classes is implemented by series of concrete factory classes. The Abstract Factory Pattern defers the creation of the concrete products to the concrete factory classes that implements the abstract factory. The client object is decoupled from the concrete product classes and the concrete factory classes through the Abstract Factory interface. The core of the Abstract Factory Pattern is to create a group of related objects that might have different implementations. The system is therefore independent of the implementation of the product types. The Abstract Factory Pattern also enables the system to replace a set of related product classes with another set, by changing the concrete factory class.

The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client.

  • The AbstractFactory defines a factory type that list operations for creating the set of related abstract product types.
  • The ConcreteFactory implements the list of operations and create the concrete product objects that are associated with the specific concrete factory class.
  • The AbstractProduct declares the operations available for the specific product type. The implementation of the product type is delegated to the subclasses of the product type.
  • The ConcreteProduct is created by a specific concrete factory object and is the realization of a specific product type. The client system is decoupled from the actual concrete product class, but invokes the implementation through the abstract product.
  • The Client object is decoupled from the ConcreteFactory and ConcreteProduct objects and work with the interfaces declared by the AbstractFactory and AbstractProduct types.

The Abstract Factory Pattern can be implemented using the Factory Method Pattern, Prototype Pattern or the Singleton Pattern. The ConcreteFactory object can be implemented as a Singleton as only one instance of the ConcreteFactory object is needed.

Structure

Java Sample Code

Download: Greek Salad Instruction

The following example illustrates the use of the Abstract Factory pattern. The Greek Salad Instruction example illustrates the creation of a family of related objects that supply the instructions for making different types of Greek Salads.

The example consists of the following classes:

  • SaladInstructionsKit.java - (AbstractFactory)
  • DicedGreekSaladInstructionFactory.java - (ConcreteFactory)
  • SlicedGreekSaladInstructionFactory.java - (ConcreteFactory)
  • CucumberInstructions.java - (AbstractProduct)
  • TomatoInstructions.java - (AbstractProduct)
  • SlicedTomatoInstructions.java - (ConcreteProduct)
  • DicedCucumberInstructions.java - (ConcreteProduct)
  • DicedTomatoInstructions.java - (ConcreteProduct)
  • SlicedCucumberInstructions.java - (ConcreteProduct)
  • GreekSaladInstructionsClient.java - (Client)
  • MainClass.java - (class contains main method)

GreekSaladInstructionsClient.java
The GreekSaladInstructionsClient class makes use of the AbstractFactory and AbstractProduct classes to determine the instructions on making a Greek Salad. Depending on the ConcreteFactory, the salad may be diced or sliced. The class will invoke the appropriate methods on the concrete product classes through the abstract product types.

SaladInstructionsKit.java
The SaladInstructionsKit defines a factory type that list operations for creating the set of related abstract product types. The Abstract Factory defers the creation of the concrete product classes to the concrete factory classes. Each specific concrete factory class will create a specific concrete product class.

TomatoInstructions.java
The TomatoInstructions declares the operations available for the specific product type. The implementation of the product type is delegated to the subclasses of the product type.

CucumberInstructions.java
The CucumberInstructions declares the operations available for the specific product type. The implementation of the product type is delegated to the subclasses of the product type.

SlicedGreekSaladInstructionFactory.java
The SlicedGreekSaladInstructionFactory implements the list of operations and create the concrete product objects that is associated with the specific concrete factory class. The concrete factory creates specific concrete product classes to create a sliced Greek Salad.

SlicedCucumberInstructions.java
The SlicedCucumberInstructions is created by a specific concrete factory object and is the realization of a specific product type.

SlicedTomatoInstructions.java
The SlicedTomatoInstructions is created by a specific concrete factory object and is the realization of a specific product type.

Design Pattern, Abstract Factory Pattern, GOF
 
 

Sequence Diagram

Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler

 

Abstract Factory vs. Factory Method

The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract Factory Pattern and the Factory Method Pattern decouples the client system from the actual implementation classes through the abstract types and factories. The Factory Method creates objects through inheritance where the Abstract Factory creates objects through composition.

See Factory Method Pattern.

References

  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995
Factory (object-oriented programming) Abstract factory pattern Design

Published at DZone with permission of Andre Mare. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Builder Design Pattern in Java
  • Abstract Factory Pattern In Java
  • Understanding the Circuit Breaker: A Key Design Pattern for Resilient Systems
  • Designing Scalable Multi-Agent AI Systems: Leveraging Domain-Driven Design and Event Storming

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: