Click here to Skip to main content
11,648,785 members (81,766 online)
Click here to Skip to main content

Software Design Patterns (Simplified) - Abstract Factory - Part 2

, 26 Aug 2005 21.1K 21
Rate this:
Please Sign up or sign in to vote.
a.k.a (Dr.D and his adventures in Software Design)
Recap: (Refer for the "Part 1" ... in the previous BLOG)
 
  Dr.D was asked to build a Car factory where the rich tycoon could build "Sports Cars" and "Family Cars" at his will. After building a car (which comes out of a factory), he would test ride it just in case. Dr.D's design impressed the Tycoon. Now Dr.D has to start coding the Car Factory and show results or else it is Goodbye cruel World for him.
 
  Anyways, lets peek at what Dr.D is doing in his most covert "Pattern Room".
 
(Scene Changes: Pattern Room) - Dr.D is staring at the Drawin Board where he had scribbled the design last time.
 
Dr.D : OK now I have to start coding this. Lemme do this one chunk at a time. What do we have here. (Refer the Figures of last blog), I have got a "CarFactoryX" which is the blueprint, and it should be an "Abstract Class".
 
(Readers, Abstract Class is one which cannot be instantiated, you cannot create an object out of it. Its only purpose in Life is to serve as the Base (Parent) class for other classes.)
 
Dr.D : I am gonna use....ummm...lemme pick a random language...C# as the coding language. Now to put the "CarFactoryX" "Absract Factory" Class in place.
 
Code Fragment 1:
 
using System;
 
//Abstract Factory
abstract class CarFactoryX
{
 // Abstract Methods returing Abstract Products
 abstract public CarFrameX Create_Car_Frame();
 abstract public CarInteriorsX Create_Car_Interiors();
 abstract public CarAccessoriesX Create_Car_Accessories();
}
 
Dr.D : Cool I have the "Abstract Class" and the "Abstract Methods" which creates (or returns) other the "Abstract Classes". Sounds very abstract ain't it? Smile | :)   The "CarFactoryX" class is like a blueprint, so whichever class inherits the abstract class, MUST implement the three methods. Or else the derived class is not worth to be called as a Car Factory. I think you got the hang of it. OK now, I am gonna do the same thing as above, for the rest of the "abstract product classes.
 
Code Fragment 2:
 
//Abstract Products
abstract class CarFrameX
{
 //Methods
 abstract public void MakeFrame();
}
 
abstract class CarInteriorsX
{
 //Methods
 abstract public void MakeInteriors();
}
 
abstract class CarAccessoriesX
{
 //Methods
 abstract public void MakeAccessories();
}
 
Dr.D : What should I do next? Right, I have to implement the two Abstract factories.
 
Code Fragment 3:
 
//Concrete Factories
class CarFactorySports : CarFactoryX
{
 // Methods
 override public CarFrameX Create_Car_Frame()
 {
  return new CarFrameSleek();
 }
 
 override public CarInteriorsX Create_Car_Interiors()
 {
  return new CarInteriorsRoyale();
 }
 
 override public CarAccessoriesX Create_Car_Accessories()
 {
  return new CarAccessoriesDVD();
 }
}
 
class CarFactoryFamily : CarFactoryX
{
 // Methods
 override public CarFrameX Create_Car_Frame()
 {
  return new CarFrameBox();
 }
 
 override public CarInteriorsX Create_Car_Interiors()
 {
  return new CarInteriorsPlain();
 }
 
 override public CarAccessoriesX Create_Car_Accessories()
 {
  return new CarAccessoriesCD();
 }
}
 
Dr.D : There. Now that I have the "Concrete Factories", I should code the "Concrete Products".
 
Code Fragment 4:
 
//Sports Concrete Products
class CarFrameSleek : CarFrameX
{
 // Methods
 override public void MakeFrame()
 {
  Console.WriteLine( "\nSleek Frame Ready...");
 }
}
 
class CarInteriorsRoyale : CarInteriorsX
{
 // Methods
 override public void MakeInteriors()
 {
  Console.WriteLine( "\nRoyale Interiors Ready...");
 }
}
 
class CarAccessoriesDVD : CarAccessoriesX
{
 // Methods
 override public void MakeAccessories()
 {
  Console.WriteLine( "\nDVD Accessories Ready...");
 }
}
 
//Family Concrete products
 
class CarFrameBox : CarFrameX
{
 // Methods
 override public void MakeFrame()
 {
  Console.WriteLine( "\nBox Frame Ready...");
 }
}
 
class CarInteriorsPlain : CarInteriorsX
{
 // Methods
 override public void MakeInteriors()
 {
  Console.WriteLine( "\nPlain Interiors Ready...");
 }
}
 
class CarAccessoriesCD : CarAccessoriesX
{
 // Methods
 override public void MakeAccessories()
 {
  Console.WriteLine( "\nCD Accessories Ready...");
 }
}
 
Dr.D: Now on for the Client...that is the "TycoonCar".
 
Code Fragment 5:
 
// Le Client's Car with ze Dough $$$
 
class ClientTycoonCar
{
 //Reference to the Abstract Products.
 private CarFrameX carFrame;
 private CarInteriorsX carInterior;
 private CarAccessoriesX carAccessories;
 
 // Create the products from the selected factory
 public ClientTycoonCar( CarFactoryX carFactory )
 {
  carFrame = carFactory.Create_Car_Frame();
  carInterior = carFactory.Create_Car_Interiors();
  carAccessories = carFactory.Create_Car_Accessories();
 }
 
 // Make the CAR from the Factory and RIDE it. This is the Recipe.
 public void TakeItForARide()
 {
  carFrame.MakeFrame();
  carInterior.MakeInteriors();
  carAccessories.MakeAccessories();
  Console.WriteLine( "\nCar is ready for test drive...VRROOOM");
 }
}
 
Dr.D : Lemme code the Test class which acts as the Tycoon. Better Safe than sorry Smile | :)
 
Code Fragment 6:
 
//Test ze Code
class TestTheCode
{
 public static void Main( string[] args )
 {
  Console.WriteLine( "\n**********************************");
  Console.WriteLine( "Tycoons Car Factory\n");
  Console.WriteLine( "**********************************");
  
  
  Console.WriteLine( "\nI am gonna make a Family Car...");
  
  ///
  // Create and take Family Car for a TestDrive
  ///
  CarFactoryX family = new CarFactoryFamily();
  ClientTycoonCar bossTest = new ClientTycoonCar ( family );
  bossTest.TakeItForARide();
  
  Console.WriteLine( "\n**********************************");
  Console.WriteLine( "\n\nHey now, I am gonna make a Sports Car...");
 
  ///
  // Create and take Sports Car for a TestDrive
  ///
  CarFactoryX sports = new CarFactorySports();
  bossTest = new ClientTycoonCar ( sports );
  bossTest.TakeItForARide();
  
  Console.WriteLine( "\n**********************************");
 
  Console.Read();
 }
}
 
And whaddya get...Ta da...the output:
 
Sample screenshot
 
Now that Dr.D has coded the CarFactory using the "Abstract Factory" Design pattern, Dr.D decides to show it to the Tycoon. The Tycoon is impressed and he decides to throw a party and the inauguration event next day.
 
(Next day at the Party...)
 
Tycoon: Well hello Dr.D...thanks a lot...my father's dream came true...I am gonna double your salary...
 
Dr.D gets very elated...
 
Tycoon: ...Next year...If I remember these words..heh heh.
 
(BAAAM...heard a heart break somewhere)
 
Dr.D : Ummm...Well thanks ...(for nothing)...
 
Tycoon: Say Doc, I know this close friend of mine who has a problem with GOD knows what. Would you be interested to help him out? Hey I can't take a "NO" for an answer...you know that.
 
Dr.D : (Here I go again)...Well sure, I will tackle the problem next week...But first, I really need a break. Seeya then. (Phew)
 

Smile | :)

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

Share

About the Author

KJAM2005
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.

You may also be interested in...

Comments and Discussions

 
GeneralMy Vote For Your way of presentation Pin
gaurav200715-Mar-09 2:17
membergaurav200715-Mar-09 2:17 
GeneralMy vote of 2 Pin
Ben_Desjardins16-Dec-08 4:03
memberBen_Desjardins16-Dec-08 4:03 
GeneralOther Design Patterns Pin
.NET Enthusiast24-Jul-08 2:59
member.NET Enthusiast24-Jul-08 2:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150804.4 | Last Updated 26 Aug 2005
Article Copyright 2005 by KJAM2005
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid