Abstract Factory – A Creational Design Pattern

Creational Design Pattern – Initial Concept

This springcavaj – Creational Design Pattern page briefly describes the Initial Concept of Creational Design Patterns, its advantages, disadvantages, its benefits, various types, common problems and few interview questions discussed briefly.

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 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 Pattern?

  • 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 Pattern?

  • 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 Pattern?

  • 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 textboxes.
  • 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 on implementation

I have developed a small program to help you to understand the working principle of Abstract Factory Design Pattern. The code is uploaded in 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 as AbstractFactoryMasterclassApplication.java in the package as com.springcavaj.designpattern.abstractfactory. Please find below the screenshot attached.

Here you can see that the 1st class under the com.springcavaj.designpattern.abstractfactory package is AbstractFactoryMasterclassApplication.java. Just right click on the class and select Run As option and then Java Application. It will run and will generate the below output.

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 concept of Mobile and Tablet. These are the 2 factories 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 class for each factory class as 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 class as MobileFactory.java, TabletFactory.java, both are interfaces. In MobileFactory.java interface it has 2 methods named as makeMobile() and playMobileGames(). And in TabletFactory.java it has only one method named as playGames().

And it has 2 implementation classes as AppleMobileFactory.java implementing both the interfaces MobileFactory.java, TabletFactory.java and another one is SamasungMobileFactory.java and it implements the same above 2 interfaces.

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();
		mobile = mobileFactory.playMobileGames();
		tablet = tabletFactory.playGames();
	}
	
	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 clearly provided in this README.md file, but still I am providing the steps to run in below bullet points.

  • Clone the sprincavaj-designpattern application from the GitHub repository.
  • Import the application as 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

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *