Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Design & Architecture » The GOF Abstract Factory Design Pattern In C#

The GOF Abstract Factory Design Pattern In C#

This article covers the basics of the GOF Abstract Factory design pattern by looking at building Model T automobiles.

Author Rank :
Page Views : 42100
Downloads : 681
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
AbstractFactorySample.zip
 
 
DevExpress Free UI Controls
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Part I. Abstract Factory Overview

The abstract factory is a GOF (Gang of Four) creational pattern where the intent is to "...provide an interface for creating families of related or dependent objects without specifying their concrete classes". ("Design Patterns" -- Gamma, Help, Johnson, Vlissides)

There are four main parts to any abstract factory:

  1. Abstract Factory: defines generic interface for creation of objects.
  2. Concrete Factory: implementation of the abstract factory.
  3. Abstract Product: the generic interface that defines the object to be created.
  4. Concrete Product: the implementation of the abstract product.  In other words, the actual objects.

When the abstract factory is ready for consumption, we will be coding to the abstract members: the abstract factory and the abstract product.  As a general rule, if our code base is abstract it should be abstract all the way.  Maintenance can be a nightmare if we have concrete implementations for interfacing with some code and abstract classes and interfaces for other parts of the code (how would we then keep track of which is which and manage change?).  The actual implementation will exist in the concrete factory and the concrete product.

The abstract factory can be implemented either through abstract base classes or interfaces.  I often stand on the "interface soapbox" because it is often the best way to keep the code base flexible and have some structure at the same time.  However, sometimes we don't want the rigidity that comes with a framework defined through interfaces, and so go with the alternative which is to use abstract base classes.  Such is the case with the provider pattern used extensively in the .NET 2.0 framework, which is basically a configurable implementation of the abstract factory pattern.  (But this is a subject for another article.)

If you have not seen it already, here's a short article I wrote on interface development.

Part II. Implementation: an Abstract Automobile Factory.

Loosely based on an article at wikipedia on the ModelT, we'll be implementing an abstract factory to build automobiles.  (http://en.wikipedia.org/wiki/Ford_Model_T)   I'll be taking quite a few liberties and so all of you "car people" out there will probably be wincing with my lack of automobile knowledge, but please remember, the point here is our factory, not the actual cars we make.

To get started, we'll look at a car in terms of four key areas: the automobile body, engine, suspension, and transmission.  These are the definitions for our abstract products.

At the core of everything is our IAuto interface which defines a (very) generic automobile.

 

// THE AUTOMOBILE -----------------------

interface IAuto

{

    IAutoBody Body { get; }

    IAutoEngine Engine { get; }

    IAutoSuspension Suspension { get; }

    IAutoTransmission Transmission { get; }

 

    void Set(IAutoBody body);

    void Set(IAutoEngine engine);

    void Set(IAutoSuspension suspension);

    void Set(IAutoTransmission transmission);
}

Next we have definitions for each of the parts:

// THE BODY ----------------------------------------

enum BodyType

{

    Car,

    Convertable,

    Sedan,

    Coupe,

    Truck

}

// THE ENGINE -----------------------

 

// THE SUSPENSION ---------------------

// THE TRANSMISSION ----------------------

enum DriveType

{

    FrontWheelDrive,

    RearWheelDrive

}

 

enum TransmissionType

{

    Manual,

    Automatic

}


 
Finally, we'll build our definition for the abstract factory.

interface IAutoFactory

{
    IAuto GetAuto();
    IAutoBody
CreateBody();

    IAutoEngine CreateEngine();

    IAutoSuspension CreateSuspension();

    IAutoTransmission CreateTransmission();

}

Part III. Implementation: the Concrete Products.

For our Model T, we need concrete definitions for the automobile and each of the parts: the engine, body, transmission and suspension.

Here is the concrete implementation of the automobile.  I'm keeping it as simple as possible here, but in real life there might be all sorts of methods and properties specific to the Model T.  We are only going to implement things at the bare minimum.

Keep in mind we are stripping out all the complexity that would exist in a real project and as you look through these, imagine what it would look like if, in the interface definition, there were five more properties and a dozen or so methods outlining how all the parts of the automobile interact and you can get an idea of why we would use an abstract factory for construction of multiple similar complex objects.

// CONCRETE AUTOMOBILE ----------------------------    

Now for the definitions of the concrete components of the Model T.  We'll keep these as simple as possible for the purposes of this article.

// CONCRETE ENGINE -----------------------------------------

 

// CONCRETE BODY ---------------------------------

    

// CONCRETE SUSPENSION ----------------------------------------------

 

 

// CONCRETE TRANSMISSION ---------------------------

Part IV. Implementation: the Concrete Factory.

Finally, we need to implement the concrete factory.  Notice that I have implemented the interface methods using the "virtual" keyword.  This is intentional, so when we want to make a new type of ModelTFactory where there is a minor change in the Model T, we can inherit from this class and make the tweaks without having to re-write the whole factory.

class ModelTFactory: IAutoFactory

    {

        #region IAutoFactory Members

 

        public IAuto GetAuto()

        {

            return new ModelT();

        }

 

        public virtual IAutoBody CreateBody()

        {

            return new ModelTBody(500, BodyType.Car, 2, false);

        }

 

        public virtual IAutoEngine CreateEngine()

        {

            return new ModelTEngine(450, 2, 35, 100, 10, "Hand Crank");

        }

 

        public virtual IAutoSuspension CreateSuspension()

        {

            return new ModelTSuspension(150, 99, 56,

                "transversely mounted semi-elliptical spring",

                "foot pedal applied band around drum");

        }

 

        public virtual IAutoTransmission CreateTransmission()

        {

            return new ModelTTransmission(100, DriveType.FrontWheelDrive,

                2, 2, TransmissionType.Manual);

        }

 

        #endregion

}

Part V. A Builder.

Now that we've covered all the bases (no pun intended), we'll create automobiles with our abstract factory using a builder method.  In a more complex scenario, the builder may possibly be responsible for wiring all the different objects together if they required it.  Note: The builder is not a part of the pattern definition, but it makes life much easier, so it is here.

static class AutomobileBuilder

{

    public static IAuto Build(IAutoFactory factory)

    {

        IAuto myAuto = factory.GetAuto();

        myAuto.Set(factory.CreateBody());

        myAuto.Set(factory.CreateEngine());

        myAuto.Set(factory.CreateSuspension());

        myAuto.Set(factory.CreateTransmission());

 

        return myAuto;

 

    }

}

Part VI. Implementing the factory.

After all the headache of creating the factory, it is actually a piece of cake to use.  Check it out:  To build a Model T, you only have to...

// Build a Model T
IAutoFactory factory = new ModelTFactory();
IAuto
 auto = AutomobileBuilder.Build(factory);

Now, where it really shines, is when we have a new version of the Model T.

In the wiki article it mentions that we can get deeper tread for the "southern road model"  so we only have to override our model T factory's CreateSuspension() method and we have a new vehicle coming from our factory (notice I also gave it "anti-lock" breaks in addition to having a tread depth of 60...  I know... anti-lock breaking systems weren't around then, but what's the fun in writing articles if you can't take some liberties every now and then).

class SouthernRoadModelTFactory: ModelTFactory

{

    public override IAutoSuspension CreateSuspension()

    {

        return new ModelTSuspension(150, 99, 60,

            "transversely mounted semi-elliptical spring",

            "anti-lock");        

    }

}

Not only can we adjust what kind of Model T is produced, but we can take advantage of our abstract factory to build ANY type of automobile: trucks, modern racing cars, anything you can think of.

Hopefully this article helped you understand the basics of the abstract factory pattern.

Until next time,

Happy coding

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Matthew Cochran
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Where is the abstract Factory? by Nat On August 20, 2007
- I think you need, an Abstract Factoy, that gives you a factory 4 u. - IAutoFactory is the interface the factories will implement, but it is not the abstract Factory. Well, of course if you have an abstract factory, you can make it to have the same IAutoFactory. - Nevertheless, The responsibility of the abstract factory will be to be a factory of factories. If you have this missing then you don't have an Abstract Factory Pattern. - you might argue, that the pattern is about switching factories. But at least the client should not depend on instatiating the factory(an IAutoFactory).
Reply | Email | Modify 
Great Article by Deepz On March 23, 2008
Hi Matthew, thanks for this great article on "Abstract Factory Design Pattern". I am a novice to Design Patterns and want to learn them ASAP. You made my journey easier by providing this wonderfull, easy-to-learn and complete article on Abstract Factory Pattern. till your next article on some other Design Pattern, Cheers !!! -- Deepak
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.