Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 356, comments - 173, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

Factory Pattern

I was asked a question last night about the factory pattern.
Factories construct and return an instance of a class type. Why you might ask, well first all the creational logic for my classes exist in one place. Secondly no modifications are needed to the factory if a new implementation is created (i.e. the factory is not tied to any specific concrete implementation).

Here is a reference implementation:

    /// <summary>
    /// Base Class for all accounts
    /// </summary>
    public abstract class Account
    {
        /// <summary>
        /// Override of the ToString so we can see what type of account we have
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return String.Format("This is a {0} account", this.GetType().FullName);
        }
    }
   
    /// <summary>
    /// CheckingAccount Implementation
    /// </summary>
    public class CheckingAccount : Account
    {
    }
   
    /// <summary>
    /// SavingsAccount Implementation
    /// </summary>
    public class SavingsAccount : Account
    {
    }   
   
    /// <summary>
    /// AccountFactory
    /// </summary>
    public class AccountFactory
    {
        /// <summary>
        /// Uses Reflection to figure out what type of account the user wants and news one up
        /// </summary>
        /// <param name="accountType">type of account the user wants</param>
        /// <returns>Account object</returns>
        public static Account CreateAccount(string accountType)
        {
            return Activator.CreateInstance(Type.GetType(accountType)) as Account;
        }
    }   

    /// <summary>
    /// Here is how I would use the factory
    /// Note: that the string literals could be comming from the database, a config file, etc
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Account a = AccountFactory.CreateAccount("FactoryTest.CheckingAccount");
            Console.WriteLine(a);
           
            a = AccountFactory.CreateAccount("FactoryTest.SavingsAccount");
            Console.WriteLine(a);

            Console.ReadKey();
        }
    }
   
  
The beauty of the Factory Pattern is let’s say for example if I add a new type of account 401kAccount. All I have to do is create that class, ensure that it inherits from the base class. Then everywhere in my code that calls the factory will be able to handle the new account type without modification. Using reflection prevents event the need to modify the Factory.   

Resources:
Design Patterns by Christopher G. Lasater Wordware Publishing (c) 2007
http://en.wikipedia.org/wiki/Factory_method_pattern
http://www.dofactory.com/Patterns/Patterns.aspx
http://www.codeguru.com/csharp/.net/net_general/patterns/article.php/c4673/

 

Print | posted on Friday, August 24, 2007 5:19 PM | Filed Under [ .NET C# ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 4 and type the answer here:

Powered by: