image
Abstract Factory Pattern

Contents

Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
4h 56m
Students
79
Ratings
5/5
starstarstarstarstar
Description

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.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

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
Students
2690
Courses
64
Learning Paths
4

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