Click here to Skip to main content
13,139,254 members (88,863 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.7K views
3 bookmarked
Posted 25 Feb 2010

C# Design Patterns: Factory

, 25 Feb 2010
Rate this:
Please Sign up or sign in to vote.
What is the factory design pattern? Well to begin with, it is something that is referenced frequently in software architecture. Functionally, a factory builds an object. More specifically, factories render instances of an abstract class type.

What is the factory design pattern? Well to begin with it is something that is referenced frequently in software architecture. Functionally, a factory builds an object. More specifically, factories render instances of an abstract class type.

C# Example

How often have you got caught up in creating objects based upon business logic? Something like this:

 public interface ICar
{
 decimal Price {get;set;}
 int HorsePower{get;set;}
}
//found somewhere randomly in your code...
 ICar result;
 //this code is heavily biased
 if(user.IsRich)
  return new AstonMartin();
 else if(user.IsSteelWorker)
  return new Ford();
 else if(user.IsPractical)
  return new Honda();
 return MomsCar();

I've found code like this in projects many times and have done it myself. What's really wrong with this?

  1. If you need this code again, and copy/paste, you'll be violating DRY (do not repeat yourself)
  2. Not reusable
  3. Not centrally based, so a change in one place will need to be copied to another
  4. Whatever class this code is in is at this point violating the Single Responsibility Principle

The Factory Design Pattern Fix

Take a look over why this is architecturally better:

public enum CarTypes { NotSet, Honda, AstonMartin, Ford };
public sealed class CarFactory
{
    public ICar Create(CarTypes cType)
    {
        if (cType == CarTypes.Honda)
            return new Honda();
        else if (cType == CarTypes.AstonMartin)
            return new AstonMartin();
        else if (cType == CarTypes.Ford)
            return new Ford();
        throw new ArgumentOutOfRangeException("cType");
    }
}

What have we changed in this example?

  1. All of our code is in one area--easy to update and reuse
  2. The factory is sealed, so we need not worry about any other classes changing the factory behavior

Simple stuff!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

micahs
Chief Technology Officer
United States United States
Pittsburgh-based developer.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMy vote of 1 Pin
Akiii00117-Mar-14 18:25
memberAkiii00117-Mar-14 18:25 
Suggestion[My vote of 2] http://www.codearsenal.net/2013/04/csharp-design-patterns-factory.html Pin
JohnDetroit6-Apr-13 4:10
memberJohnDetroit6-Apr-13 4:10 
GeneralThis doesn't seem to gain much... Pin
supercat925-Feb-10 8:38
membersupercat925-Feb-10 8:38 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 25 Feb 2010
Article Copyright 2010 by micahs
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid