In this article we will create an ASP.Net application using Abstract Factory Pattern.
Agenda
- What is Abstract Factory Pattern?
- Do a walkthrough of an ASP.Net Example written using the traditional Factory Pattern and understand the difficulties.
- How does the Abstract Factory Pattern solve the problem?
- What are the components involved in the Abstract Factory Pattern?
- Recreate the Previous code and implement using an abstract design pattern.
- Class diagram for an abstract design pattern.
Previous Articles
- Design Patterns: Introduction
- Learn Design Pattern - Singleton Pattern
- Learn Design Pattern - Factory Method Pattern
What is Abstract Design Pattern?
- An Abstract Factory is the extension of the Factory method pattern and provides an abstraction one level higher than the factory method pattern.
- In short the Abstract Factory creates an object of some other factories which at the end creates an object of concrete classes. Or we can say the Abstract Factory encapsulates individual factories having a single theme.
- Like a Factory method it falls under the creational pattern.
- According to the Gang of Four: It provides an Interface for creating families of related or dependent objects without specifying their concrete classes.
Why and When to extend Factory method to Abstract Factory Pattern?
If you haven't read Factory Method Pattern please go through it first.
Let's consider the same example we used for the Factory method pattern.
There we had a class called CustomerFactory which creates various customers according to input it gets.
Now the Company decides to extend the application and they say, all customers belonging to a specified country (country 1) and who are of Grade1 get a 200 Rs discount and Grade2 customers belonging to another country (country 2) get a discount of 100Rs, and remaining ____.
So the CustomerFactory ends up with a code which looks like this:
- public IBaseCustomer GetCustomer(string CustomerType, string Country)
- {
- switch (CustomerType)
- {
- case "1":
- switch (Country)
- {
- case "Country1":
- {
- IBaseCustomer c = new Grade1Customer();
- c.DiscountAmount = 200;
- return c;
- }
- default:
- {
- IBaseCustomer c = new Grade1Customer();
- c.DiscountAmount = 0;
- return c;
- }
- }
- case "2":
- switch (Country)
- {
- case "Country2":
- {
- IBaseCustomer c = new Grade2Customer();
- c.DiscountAmount = 100;
- return c;
- }
- default:
- {
- IBaseCustomer c = new Grade1Customer();
- c.DiscountAmount = 0;
- return c;
- }
- }
- default:
- {
- IBaseCustomer c = new Grade1Customer();
- c.DiscountAmount = 0;
- return c;
- }
- }
- }
Client Code
- CustomerFactory factory=new CustomerFactory();
- IBaseCustomer c= factory. GetCustomer("1","Country1");
- c.getAmount()//display;
-
Factory ends up with too many case (or if) conditions, and every new country results in two or more case conditions.
-
One solution is to create 3 different factories, Country1Factory, Country2Factory and OtherFactory. Which means every new country results in recompilation of client code, also the client needs to be aware of each and every factory.



Gowtham RajamanickamPosted Apr 15, 2016, 1:22 PM
Thanks for sharing
Sukesh MarlaPosted Oct 13, 2012, 11:52 PM
CustomerFactory is not static, the method is made static.its just for simplicity,not required to create object.
Sai SherlekarPosted Oct 13, 2012, 3:30 PM
why CustomerFactory class is static?? can we achieve without static?
Sukesh MarlaPosted Sep 17, 2012, 10:27 AM
Understand your problem statement, Think like, how your application is going to be extended. This is the very rarely used pattern. In Factory Pattern you abstracted initialization logic of concrete classes with the help of factories and here Factory Initialization is abstracted.
Gaurav GuptaPosted Sep 17, 2012, 12:18 AM
How can I analyze when it is being in used?