The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
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 AbstractFactory 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 abstract product is an interface defined with the attributes of the object to be created.
Abstract product is the actual object by referring to related abstract objects. And, of course, the client is an 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 found a good example of this pattern. Let's implement it. Create a new project and name it AbstractFactoryBank. 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 concrete classes to implement interface, ABCD Bank. Add constructor.
Name, ABCD Bank. Let's add second bank name will be EFGH Bank. Now, we need loanAbstract class, double rate, GetInterestedRate. 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 AbstractFactory to get the factories for bank and loan objects.
We need to method, GetBank and GetLoan. Now, create the factory classes that inherit AbstractFactory 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 LoanFactory class. Of course, we need to override GetLoan. Let's adjust case sensitivity also.
Done. Now, we need a FactoryCreator class to get the factories by parsing information such as bank or loan. In this case, we have to use the FactoryCreator to get AbstractFactory in order to get factories of concrete classes by parsing information such as type. I'm going to write codes quickly. First, we need a buffer to take value from users. First, ask bank name, get bankName.
Let's create an instance from bank. Now, we need rate, the loan amount, AbstractFactory, loanFactory, FactoryCreator, GetFactory with loan. Now, we will get loanName, GetInterestedRate, and the last step is calculate. Done. Now, let's calculate. Run program.
Bank name, loan type, rate, amount, calculate. As you 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. So, I'll see you in the next video, friend.
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.