Creational Design Pattern – Initial Concept
This springcavaj – Creational Design Pattern page briefly describes the Initial Concept of Creational Design Patterns, its advantages, disadvantages, benefits, various types, common problems, and a few interview questions discussed.
This guide will provide a deep understanding of Creational Design Patterns for designing and architecting an application.
What is an Abstract Factory Design Pattern?
The Abstract Factory Pattern is a Creational Design Pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern allows the client to create objects that follow a general interface without knowing the specifics of the object creation.
What are the features of the Abstract Factory Design Pattern?
- Encapsulates the creation of a set of related objects.
- Allows for the creation of families of related objects without specifying their concrete classes.
- Follows the “Program to an interface, not an implementation” principle.
What are the advantages of Abstract Factory Design Patterns?
- Encapsulation of Object Creation – The pattern encapsulates the creation of a family of objects, making the code easier to maintain and extend.
- Decoupling – It decouples the code that creates objects from the code that uses the objects.
- Flexibility – Adding new families of products (object types) is easier without altering the existing code.
What are the disadvantages of Abstract Factory Design Patterns?
- Complexity – The pattern adds more complexity due to the creation of multiple interfaces and classes.
- Tight Coupling to Factories – The client code is coupled to the abstract factory interface, which might require changes in the client when a new type of product is introduced.
What are the use cases of Abstract Factory Design Patterns?
- UI Toolkits – Creating a cross-platform UI toolkit where each platform (Windows, macOS, Linux) has its own set of UI components like buttons, checkboxes, and text boxes.
- Database Systems – Creating a database connection system where each database type (MySQL, Oracle, SQL Server) has its own connection and command objects.
- Document Generators – Generating documents in multiple formats (HTML, PDF, Word) where each format has its own headers, footers, and body components.
- Themes in Applications – Implementing multiple themes (dark, light, custom) for an application where each theme has its own set of UI components styled differently.
Brief description of implementation
I have developed a small program to help you to understand the working principle of Abstract Factory Design Patterns. The code is uploaded to GitHub as springcavaj-designpattern repository. Please clone the application and import in any of the IDs like either STS or Eclipse.
After successfully importing the above code base, one will find a class named AbstractFactoryMasterclassApplication.java in the package as com.springcavaj.designpattern.abstractfactory. Please see the screenshot attached below.

Here you can see the class under the com.springcavaj.designpattern.abstractfactory package is AbstractFactoryMasterclassApplication.java. Just right-click on the class, select the Run As option, and then Java Application. It will run and generate the output as provided below.
INFO com.springcavaj.designpattern.abstractfactory.design.impl.AppleMobile - makeCalls() - Calling using AppleMobile
INFO com.springcavaj.designpattern.abstractfactory.design.impl.AppleMobile - playGames() - Playing Games in AppleMobile
INFO com.springcavaj.designpattern.abstractfactory.design.impl.AppleTablet - playGames() - Playing Games in AppleTablet
INFO com.springcavaj.designpattern.abstractfactory.design.impl.SamsungMobile - makeCalls() - Calling using SamsungMobile
INFO com.springcavaj.designpattern.abstractfactory.design.impl.SamsungMobile - playGames() - Playing Games in SamsungMobile
INFO com.springcavaj.designpattern.abstractfactory.design.impl.SamsungTablet - playGames() - Playing Games in SamsungTablet
Detailed Explanation
In this small program, I have used the concepts of Mobile and Tablet, which are the 2 factory classes.
In Mobile.java interface, there are 2 methods as makeCalls() and playGames(). And in Tablet.java there is only one method as playGames().
Now it has 2 implementation classes for each factory class AppleMobile.java, AppleTable.java, and SamsungMobile.java, SamsungTablet.java. As it implements, so it overrides the methods of Mobile.java, Tablet.java respectively.
Now the Factory class, in our case I have 2 Factory classes MobileFactory.java, TabletFactory.java, both are interfaces. In MobileFactory.java interface it has 1 method named as makeMobile(). And in TabletFactory.java it has only one method named as makeTablet().
It has 2 implementation classes AppleMobileFactory.java, and SamasungMobileFactory.java which implement both the interfaces MobileFactory.java, TabletFactory.java.
Now, the advantage is that in the future, if you come up with a new Mobile named MotorolaMobile.java class, then you can implement the Mobile.java interface to override the makeMobile() method apart from that if you need any thing more like bestPhoto(), etc. Then you can add those extra methods in the MotorolaMobile.java class itself without touching the existing code.
This follows 2 principles of SOLID Principle one is Single Responsibility and another is Open for Enhancement and closed for Modification.
Now, the AbstractFactoryMasterclassApplication.java creates the object of both the Factory interfaces in the below manner.
public class AbstractFactoryMasterclassApplication {
private Mobile mobile;
private Tablet tablet;
public AbstractFactoryMasterclassApplication(MobileFactory mobileFactory, TabletFactory tabletFactory) {
mobile = mobileFactory.makeMobile();
tablet = tabletFactory.makeTablet();
}
public void humanActivities() {
mobile.makeCalls();
mobile.playGames();
tablet.playGames();
}
/**
* @param args
*/
public static void main(String[] args) {
AppleMobileFactory appleMobileFactory = new AppleMobileFactory();
AbstractFactoryMasterclassApplication abstractFactoryMasterclassApplication = new AbstractFactoryMasterclassApplication(appleMobileFactory, appleMobileFactory);
abstractFactoryMasterclassApplication.humanActivities();
SamsungMobileFactory samsungMobileFactory = new SamsungMobileFactory();
abstractFactoryMasterclassApplication = new AbstractFactoryMasterclassApplication(samsungMobileFactory, samsungMobileFactory);
abstractFactoryMasterclassApplication.humanActivities();
}
}
Steps to run the application
The complete Run application steps are provided in this README.md file, but still, I am providing the steps to run points below.
- Clone the sprincavaj-designpattern application from the GitHub repository.
- Import the application as a Maven application, either in STS or Eclipse.
- Find the AbstractFactoryMasterclassApplication.java class.
- Right-click on the file and select Run As -> Java Application.
GitHub Code Link
Download the Source Code from GitHub
Common Problems Faced Implementing Abstract Factory Pattern
Abstract Factory Design Pattern Common Problems
Interview FAQs
Abstract Factory Design Pattern Interview FAQs
Other Useful Links
Implementations of various components of Spring Boot