Click here to Skip to main content
15,867,488 members
Articles / Web Development / HTML
Article

Software Design Patterns (Simplified) - Abstract Factory - Part1

Rate me:
Please Sign up or sign in to vote.
3.22/5 (31 votes)
25 Aug 20056 min read 36.6K   27   7
a.k.a (Dr.D and his adventures in Software Design)

Introduction

  What is a Factory? A place where one creates products...OK now that definition did not come from the Oxford Dictionary...anyways lets imagine you are the long lost son of a tycoon and you inherit 100 million dollars from your father. Now you are the new Tycoon. Cool. And guess what you cannot enjoy all that money...you have to fulfill your father's last wish of making a Car Factory. One that puts Henry ford's factory to dust. After 2 days, You get revived after the shock and Call Dr.D to your house for his advice. ( Dr.D for Dr Design :-) )
 
  You also give a buzz to your (money eating) Marketing analyst who gives advice on the latest craze and trends on Cars. He explains that the public is divided into two when it comes to cars...One is the "Sports Car" which the rich young guys would prefer to take their gals for a ride to the casino and the other one is the "Family Car" on which the (pathetic) "Man of the house" has to take his family to the nearest grocery store to pick up Cereals and Vegetables for his family's next meal. Boohoo.
 
  Anyway..you are the tycoon.. and you give the requirement to Dr.D. (For your information Dr.D is this typical nerdy guy with tri-spectacled glasses on his nose who enjoys lunch with a group of physics nobel laureates discussing the mistakes which Einstein has done when calculating the Theory of Relativity.)
 
(Conversation)
 
Tycoon:  "Hey Dr.D...Listen up. I want to make a factory that produces two types of cars (a Family and a Sports one). I don't care what you do, but when I order to create a bunch of "Family cars"...I want 10 of them per day parked outside the shipment area...and when I want "Sports Cars"...they should be ready in no time. All depends on the market conditions but I should have the full control to switch over the production to any type of car anytime and use it (like give it a test ride or somethin)."
 
Dr.D  :  "Ummm OK So you want a very flexible environment where you order whichever type of car...and Lo and behold its present in the parking lot. So you are not worried about what goes inside and what products are created in making the Car."
 
Tycoon:  "You got that right, Buddy. Anyways...I want to know what yer doin in a week or else you can spend your lunch time on a job site. Catchya later."
 
So our poor Dr.D goes to home...ignores his wife...( Reminds me of "Beautiful Mind" :-) ) and goes straight to his "PATTERN ROOM"
 

Dr.D  :  "Hmmm, OK, Now, What does every car need to get to a vehicle status. Lemme jot that down."
 
PATTERN ROOM (DRAWIN BOARD)
1. Car Frame
2. Car Interiors
3. Car Accessories
 
Dr.D  :  "Lemme call this factory as "CarFactoryX". This factory is the blueprint for the upcoming Car Factories. Hey, Now that I have a blueprint, lemme draw the specialized factories..."CarFactoryFamily" and "CarFactorySports"
 
PATTERN ROOM (DRAWIN BOARD)
 
Sample screenshot
 
Dr.D  :  "Now, I need to go deeper. Each individual product, for example the Car Frame might change for each of the Car type. There will be "Sleek car frame" for the "Sports car" and "Box car frame" for the "Family Car". I gotta do that for the Car Interiors and Car Accessories as well."
 
PATTERN ROOM (DRAWIN BOARD)
 
Sample screenshot
 
Dr.D  :  "Good, I am makin progress. I have to connect the corresponding Factory to the individual products. To better understand. The methods in "CarFactoryFamily" knows what kind of "CarFrame", "CarInteriors" and "CarAccessories" it wants to produce. So naturally in the case of
 
"Family Car" :
 
> "Create_Car_Frame()" makes use of or implements "CarFrameBox" (object).
> "Create_Car_Interiors()" makes use of or implements "CarInteriorsPlain" (object).
> "Create_Car_Accessories()" makes use of or implements "CarAccessoriesCD" (object).
 
Similarly for the
 
"Sports Car" :
 
> "Create_Car_Frame()" makes use of or implements "CarFrameSleek" (object).
> "Create_Car_Interiors()" makes use of or implements "CarInteriorsRoyale" (object).
> "Create_Car_Accessories()" makes use of or implements "CarAccessoriesDVD" (object).
 
PATTERN ROOM (DRAWIN BOARD)
 
Sample screenshot
 
 
Dr.D Explains  : "Okay Guys just for your information to know technical jargon, The Classes with "X" as their Suffixes are all "Abstract Classes", that is they serve as a blue print for the other classes which are not "X". These are the "Concrete " products. Simply put, you cannot have a product called "CarFrameX". This is the blueprint or the "Abstract Product". But you surely can have a "CarFrameSleek", This is the "Concrete Product" which implements the "Abstract Product".
 
So what you have is a bunch of Abstracts (Blueprint) and Concretes (Implementation)
 
Abstract Factory (CarFactoryX)
and its related
Concrete Factory (CarFactoryFamily and CarFactorySports)
 
Abstract Product (CarFrameX, CarInteriorsX and CarAccessoriesX)
and its related
Concrete Product (CarFrameBox and CarFrameSleek,  CarInteriorsPlain and CarInteriorsRoyale, CarAccessoriesCD and CarAccessoriesDVD)
 
To add the toppings, your BOSS (Client) must have access to the blueprints (or uses those blueprints), that is the
 
Abstract Factory (CarFactoryX)
Abstract Product (CarFrameX, CarInteriorsX and CarAccessoriesX)
 
Now why does he use the blueprints, rather than the concrete ones?
 
Because, he can do the work seamlessly. Suppose the tycoon has a button to switch between the "Sports Car" and "Family Car" production at his will, he can do it without the extra hassles. Since both the type of cars implement (or are aware of) the CarFactoryX, Any type of factory can replace the CarFactoryX interface to make the desired car.
 
Lemme walk you down through the MAIN code just to get the feel of it. I will not dive in to the inner implementation right away.
 
 Line1: // Create and take Family Car for a TestDrive
 Line2: CarFactoryX family = new CarFactoryFamily();
 Line3: ClientTycoonCar bossTest = new ClientTycoonCar ( family );
 Line4: bossTest.TakeItForARide();
  
 Line5: // Create and take Sports Car for a TestDrive
 Line6: CarFactoryX sports = new CarFactorySports();
 Line7: bossTest = new ClientTycoonCar ( sports );
 Line8: bossTest.TakeItForARide();
 

Here "ClientTycoonCar" is the client. All the client needs to know is this (Here's what goes on the Tycoon's little Mind)
 
> Create me a "Family Car"            (Line 2)
> I wanna use the "Family Car"      (Line 3)
> Take the "Family Car" for a testdrive     (Line 4)
 
> Create me a "Sports Car" this time..Heh heh :-)  (Line 5)
> I wanna use the "Sports Car"           (Line 6)
> Take the "Sports Car" for a testdrive          (Line 7)
 

...Next Day at the Tycoon's House
 
Tycoon: "Hey Dr.D...did ya come up with some idea fer me."
 
Dr.D  : "Sure, Have a look at the design."
 
Tycoon: "Looks great, when do we start coding."
 
Dr.D  : "Wow, Do you know coding, I thought big shots like you will never..."
 
Tycoon: "YadaYadaYada...I was jokin Doc...yer the one who is gonna code. So start it tomorrow first thing in the morning, else I suggest you look through the Vacancy Ads in the newspaper on your way out...Alright, Enjoy the night...Will meet ya tomorrow.See ya"
 
Dr.D  : "Jeez, Thanks but what about today's Pay..."
 
(DOOR SLAAAAAM right in the docs face)
 
Dr.D  : "...ment."
 
:-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
I am a fun/brainy and religious soul. Calm (in nature), Cool (in tense situations) and Collected (when making tough decisions). I am a Software guy by birth Smile | :) and I love to connect to people of various cultures, religions and places.

Comments and Discussions

 
QuestionWhen you will write new story ? Pin
Mathiyazhagan26-May-09 2:39
Mathiyazhagan26-May-09 2:39 
QuestionWhy is the voting so low ? Pin
Martin Bohring25-Aug-05 22:59
Martin Bohring25-Aug-05 22:59 
AnswerRe: Why is the voting so low ? Pin
CWinThread29-Aug-05 4:56
CWinThread29-Aug-05 4:56 
GeneralRe: Why is the voting so low ? Pin
KJAM200529-Aug-05 17:34
KJAM200529-Aug-05 17:34 
GeneralRe: Why is the voting so low ? Pin
ThatsAlok14-Nov-06 3:37
ThatsAlok14-Nov-06 3:37 
GeneralRe: Why is the voting so low ? Pin
nik4u5-Dec-07 22:38
nik4u5-Dec-07 22:38 
GeneralRe: Why is the voting so low ? Pin
ancich17-Dec-19 5:52
ancich17-Dec-19 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.