Abstract Factory Design Pattern is a creational design pattern, it is some high level design pattern that factory method design pattern. According to this design pattern, you just define an interface or abstract class to create related dependent object without specifying its concrete sub class. So here abstract factory return a factory of classes.
Let me more simply it for understanding, you have a set of factory method design pattern, you just put into under a factory using factory design pattern, means it is simply factory of factories. And you no need to know about all factories into factory, you can make your program using top level factory.
According to the Gang of Four:
“Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”
In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.
Let’s see the following class diagram for the Abstract Factory Design Pattern, it illustrates all about its component classes and interfaces.
Let’s see the classes and objects participating in this pattern are:
AbstractFactory (AbstractFactory )
ConcreteFactory (BankFactory, AccountFactory)
AbstractProduct (Account, Bank)
Product (SavingAccount, CurrentAccount, ICICIBank, YesBank)
Client (AbstractFactoryPatternMain)
There are following benefits of using abstract factory design pattern.
also read:
There are following disadvantages of this pattern.
For example, in Spring Framework, you have one of FactoryBean implementation is LocalSessionFactoryBean, in order to get a reference of a bean that was associated with hibernate configuration, the specific configuration about DataSource should applied before get an object of SessionFactory. You can use the LocalSessionFactoryBean to apply the specific DataSource configuration in a consistent way. You may inject the result of a FactoryBean’s getObject() method into any other property.
I am going to create a Bank and Account interfaces and concrete classes implementing these interfaces. Here I also create an abstract factory class AbstractFactory. I have some factory classes BankFactory and AccountFactory, these classes extends AbstractFactory class. And also I create a class FactoryProducer to create the factories.
Bank.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public interface Bank { void bankName(); }
ICICIBank.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class ICICIBank implements Bank { @Override public void bankName() { System.out.println("ICICI Bank Ltd."); } }
YesBank.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class YesBank implements Bank { @Override public void bankName() { System.out.println("Yes Bank Pvt. Ltd."); } }
Account.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public interface Account { void accountType(); }
SavingAccount .java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class SavingAccount implements Account { @Override public void accountType() { System.out.println("SAVING ACCOUNT"); } }
CurrentAccount.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class CurrentAccount implements Account { @Override public void accountType() { System.out.println("CURRENT ACCOUNT"); } }
AbstractFactory.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public abstract class AbstractFactory { abstract Bank getBank(String bankName); abstract Account getAccount(String accountType); }
BankFactory.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class BankFactory extends AbstractFactory { final String ICICI_BANK = "ICICI"; final String YES_BANK = "YES"; @Override Bank getBank(String bankName) { if(ICICI_BANK.equalsIgnoreCase(bankName)){ return new ICICIBank(); } else if(YES_BANK.equalsIgnoreCase(bankName)){ return new YesBank(); } return null; } @Override Account getAccount(String accountType) { return null; } }
AccountFactory.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class AccountFactory extends AbstractFactory { final String CURRENT_ACCOUNT = "CURRENT"; final String SAVING_ACCOUNT = "SAVING"; @Override Bank getBank(String bankName) { return null; } @Override public Account getAccount(String accountType){ if(CURRENT_ACCOUNT.equals(accountType)) { return new CurrentAccount(); }else if(SAVING_ACCOUNT.equals(accountType)){ return new SavingAccount(); } return null; } }
FactoryProducer.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class FactoryProducer { final static String BANK = "BANK"; final static String ACCOUNT = "ACCOUNT"; public static AbstractFactory getFactory(String factory){ if(BANK.equalsIgnoreCase(factory)){ return new BankFactory(); }else if(ACCOUNT.equalsIgnoreCase(factory)){ return new AccountFactory(); } return null; } }
AbstractFactoryPatternMain.java
/** * */ package com.doj.patterns.creational.abstractfactory; /** * @author Dinesh.Rajput * */ public class AbstractFactoryPatternMain { /** * @param args */ public static void main(String[] args) { AccountFactory accountFactory = new AccountFactory(); Account savingAccount = accountFactory.getAccount("SAVING"); savingAccount.accountType(); Account currentAccount = accountFactory.getAccount("CURRENT"); currentAccount.accountType(); } }
SAVING ACCOUNT CURRENT ACCOUNT
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…