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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • Application Architecture Design Principles
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • The Ultimate API Development Guide: Strategy, Tools, Best Practices

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • Application Architecture Design Principles
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • The Ultimate API Development Guide: Strategy, Tools, Best Practices

Intro to Design Patterns: Abstract Factory Pattern

Andre Mare user avatar by
Andre Mare
·
Apr. 30, 08 · News
Like (0)
Save
Tweet
Share
27.76K 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.

Trending

  • Automating the Migration From JS to TS for the ZK Framework
  • Application Architecture Design Principles
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • The Ultimate API Development Guide: Strategy, Tools, Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: