Abstract Factory Pattern
Start course
4h 56m

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.



Hello dear friends. In this video we will examine the Abstract Factory Pattern. So, let's begin. The Abstract Factory Pattern is a creational pattern in which interfaces are defined for creating families of related objects without specifying their actual implementations. When using this pattern, you create factories which return many kinds of related objects. Basically, in the Abstract Factory Pattern we get rid of if/else blocks, and have a Factory class for each subclass. Then an Abstract Factory class that will return the subclass based on the Input Factory class. At first, it seems confusing, but once you see the implementation, it's really easy to grasp and understand the minor differences between Factory and Abstract Factory Pattern. We can see the UML diagram of the Abstract Factory Pattern.

As you can see, an Abstract Factory is an abstract container which creates abstract objects. Concrete Factory implements an abstract container to create concrete objects. See, now the AbstractProduct is an interface defined with the attributes of the object to be created. AbstractProduct is the actual object by referring related abstract objects. And of course, Client is application that creates families of related objects using the Factory. Now, let's create a little project and implement this pattern. Actually on a website, I have found a good example of this pattern. Let's implement it. Create a new project and name it AbstractBank. With this project, we will calculate the loan payment for different Banks, let's start. First, we need an interface. Let's call it Bank, String GetBankName.

Now we need to concrete classes to implement interface, ABCD Bank. Add constructor, name, ABCD Bank. Let's add second bank, name will be EFGH Bank. Now last bank, maybe WXYZ Bank. Done. Now we need LoanAbstraction class, double rate, GetInterestRate, and of course, we need to calculate. Let's calculate. We can use Math class. Done, let's show the result. Done, add a new class to use loan, HomeLoan, BusinessLoan, and last one will be EducationLoan. Create an abstract class as Abstract Factory to get the factories for Bank and Loan objects. We need two method GetBank and GetLoan. Now create the Factory classes that inherit Abstract Factory class to generate the object of concrete class based on given information. BankFactory extends from AbstractFactory.

If you want, we can adjust key sensitivity for bank name. Done, let's add loan Factory class. Let's adjust case sensitivity also. Done. Now, we need a factory creator class to get the factories by passing an information such as bank or loan. In this case, we have to use the factory creator to get abstract factory, in order to get factories of concrete classes by passing an information such as type. I will write codes quickly. First, we need a buffer to take value from users. First ask bank name. Get bank name. Let's create an instance from bank. Now we need rate. The loan amount. AbstractFactory loanFactory = FactoryCreator.GetFactory ("Loan"). Now we will get loan name. GetInterestedRate and the last step is calculate. Done, now let's calculate run program. Bank name, loan name, rate, amount, calculate. As you can see, we can calculate without any problem. I think that's enough about abstract factory pattern. Next video, we will examine the builder design pattern. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics