Friday, September 20, 2013

Abstract Factory Design Pattern

Few days ago I had to help my friend to do his college assignment. He is new to programming field but he was asked to develop a java program to implement a system which includes Abstract Factory Design Pattern.

I didn't have any experience with abstract factory design pattern. The only thing I know was that it is a Creational Design pattern. So I did some research on that design pattern. But I felt abstract factory design is bit difficult to understand. So I decided to write a blog post on my experience. So this will be an comparison between Factory pattern and the Abstract Factory pattern.

The first thing which I had to consider is the difference between the Factory Design pattern and the Abstract Factory design pattern. Both are bit similar. But Abstract Factory  design pattern is one step ahead than the factory design pattern.

The Factory design pattern only hides the instantiation logic from the client. When the user ask for a new object the factory class calls createObject() method and returns a instance of that object. That instance is what only see by the user. He does not know the instantiation logic. Below diagram will show the logic.


Abstract Factory design pattern is one step above than the Factory pattern. In the Factory design pattern you only gave an interface to handle one object. But this pattern gives an interface to creating few related objects. Simply it hides the factory. Below diagram will show the logic.


But that diagram does not describe the whole thing. So here after I will describe my implementation.

In that assignment, he was asked to implement a system for banking environment including accounts, loans and staff. Most importantly, he has to use Abstract Factory design pattern.

First I built three abstract classes for Account, Loan and Staff. By extending them I built Savings, Current and Kids Accounts, Personal and Housing loans and Temporary and Permanent Staff classes.

Then I implemented Three Factory classes which included Factory design pattern. Those factory classes are Account handler, Loan Handler and Staff Handler. Those will do the job of Factory design pattern. Single handler class will do the abstraction of one type of object.(ex: AccountHandler will hides the implementation of Account objects.)

Then my task was to implement the Abstract Factory pattern. So I considered a scenario of two banks called Bank A and Bank B. Generally both banks have the save structure. So I used an abstract class called AbstractBankFactory. By extending that class you can build any bank. Those bank classes will handle the abstraction of their internal operations.

Now the client can easily call bank.createAccount() method to create an Account.  

Below code is the AbstractBankFactory



And this is how the user should use it.


User only needs to call bank.createAccount() method to create. Also if he needs to take a loan, he will need to call bank.takeLoan(). If he needs to deposit money, he will need to call bank.deposit(accountNo). This method is just like the real banking environment. When the client go to the bank, he does not need to how the internal operations are executed. He only needs to get done what he wanted. When he call bank.createAccount() bank will return a bank account for that user. When he wants to get a loan, he can call bank.getLoan(variables) and pass necessary variables, bank will return a loan for that customer. This is just like the factory pattern. But in the factory pattern you only hides one object type. But in this implementation you can hide the implementation of both Account and Loan objects. Simply it hides the factory called bank.

In this blog post I wanted to share my experience on Abstract Factory design pattern. Because in most articles which I came across didn't have proper description. Those had only the basic idea. Also this blog post differentiate the Factory Design Pattern and the Abstract Design Pattern.

If you have any concern please feel free to post a comment below. Also if you need the complete code, please mention it in the comment. Thank you. 

No comments:

Post a Comment