A simple C# factory example

This simple example is an attempt at demonstrating the factory pattern. The key point of the following code is that the client (Program.Main) doesn’t need to know about the state of the producers (Gus and TheMan) in order to get products (Widgets vs MegaWidgets).

The following screen shows the application running, you can download the sources from here.

Five lines of console output, fourth line is: Help I'm trapped in a code factory.

The following code is my fun example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelpImTrapped
{
    // abstract class for the products
    abstract class Product
    {
        protected string _producerNotes;
        protected int _value;
        protected HashSet<string> payload;

        virtual public void setNotes(string notes){
            _producerNotes = notes;
        }

        virtual public string getNotes()
        {
            return _producerNotes;
        }

        public override string ToString()
        {
            return "value: " + _value.ToString() + " " + _producerNotes;
        }
    }

    // abstract Creator / Producer
    abstract class Producer
    {
        protected static int id = 0;
        virtual public Product CreateProduct()
        {
            return null;
        }
    }

    // concrete class for the products created by the man
    class Widget : Product
    {
        public Widget(int val)
        {
            _value = val;
        }
    }

    // concrete producer, the man
    class TheMan : Producer
    {
        public override Product CreateProduct()
        {
            Product p = new Widget(id++);

            p.setNotes("These workers aren't working very hard, look at all my widgets.");

            return p;
        }        
    }

    // the "more mega" widgets that are
    // created by Gus
    class MegaWidget : Product
    {
        public MegaWidget(int val)
        {
            _value = (val * (1024*1024));
        }
    }

    // concrete producer, me
    class Gus : Producer
    {       
        private bool theManLooking = false;
        public override Product CreateProduct()
        {
            Product p;
            if (theManLooking)
              p = new MegaWidget(id++);
            else
              p = new Widget(id++);

            p.setNotes("Help!  I'm trapped in a code factory!");

            return p;
        }
    }

    class Program
    {
        // The main loop will just instantiate some products using the factory methods
        // of concrete producers
        static void Main(string[] args)
        {
            TheMan daMan = new TheMan();
            Gus g        = new Gus();
            Product[] products = { daMan.CreateProduct(), daMan.CreateProduct(), daMan.CreateProduct(), g.CreateProduct(), daMan.CreateProduct() };

            foreach (Product product in products)
            {
                System.Console.WriteLine(product);
            }

            System.Console.ReadLine();
        }
    }
}

Although simple and just barely a factory, this example serves to demonstrate something: using designs like this for managing the creation of objects makes for much more flexible and extensive code while adding very little complexity. The main benefit is that you will get the right class produced depending on the state of the producer (e.g. if TheMan is looking, you get a MegaWidget as opposed to a Widget). There are additional benefits as well though. For example, if somewhere down the line, we wanted to make significant changes to how Gus and TheMan are creating large bundles of various types of Products, it would greatly simplify the work that a client (like this Program’s Main) would have to do to handle the updates while still encapsulating all of the changes to Gus or TheMan.