Design patterns in C# – part 9 (Factory method pattern)

Role

The Factory Method pattern is a way of creating objects, but letting subclasses decide exactly which class to instantiate.

Design

FactoryMethodPattern

Implementation

  • Client -> TestFactoryMethodPattern
  • IProduct -> IToyotaModel
  • Creator -> CarShowroom
  • ProductA, ProductB -> Yaris, Corolla, RAV4, LandCruiser, NotAvailable
  • FactoryMethod -> GetOffer
namespace FactoryMethodPattern
{
    public interface IToyotaModel
    {
        string Name { get; }
        uint Price { get; }
    }
}
namespace FactoryMethodPattern
{
    internal class Yaris : IToyotaModel
    {
        #region IToyotaModel Members

        public string Name
        {
            get { return "Toyota Yaris"; }
        }

        public uint Price
        {
            get { return 12205; }
        }

        #endregion
    }

    internal class Corolla : IToyotaModel
    {
        #region IToyotaModel Members

        public string Name
        {
            get { return "Toyota Corolla"; }
        }

        public uint Price
        {
            get { return 15350; }
        }

        #endregion
    }

    internal class RAV4 : IToyotaModel
    {
        #region IToyotaModel Members

        public string Name
        {
            get { return "Toyota RAV4"; }
        }

        public uint Price
        {
            get { return 21500; }
        }

        #endregion
    }

    internal class LandCruiser : IToyotaModel
    {
        #region IToyotaModel Members

        public string Name
        {
            get { return "Toyota Land Cruiser"; }
        }

        public uint Price
        {
            get { return 64755; }
        }

        #endregion
    }

    internal class NotAvailable : IToyotaModel
    {
        #region IToyotaModel Members

        public string Name
        {
            get { return "No Car"; }
        }

        public uint Price
        {
            get { return 0; }
        }

        #endregion
    }
}
namespace FactoryMethodPattern
{
    public class CarShowroom
    {
        public IToyotaModel GetOffer(uint price)
        {
            if (price < (new Yaris()).Price)
            {
                return new NotAvailable();
            }
            if (price < (new Corolla()).Price)
            {
                return new Yaris();
            }
            if (price < (new RAV4()).Price)
            {
                return new Corolla();
            }
            if (price < (new LandCruiser()).Price)
            {
                return new RAV4();
            }

            return new LandCruiser();
        }
    }
}

using System;

namespace FactoryMethodPattern
{
public class TestFactoryMethodPattern
{
private static void DisplayTalk(CarShowroom carShowroom, uint price, string clientName)
{
Console.WriteLine("\n\n\t{0} visit the toyota car showroom\n", clientName);
Console.WriteLine("Dealer: Hello {0}. How many Dollars would you spend?\n", clientName);
Console.WriteLine("{0}: Hi, I have for it {1} dollars. What can you offer to me ?\n", clientName, price);
Console.WriteLine("Dealer: Ohw, I can offer you: {0}. It is the best I can offer to you.\n",
carShowroom.GetOffer(price).Name);
}

public static void Main(string[] args)
{
var toyotaShowroom = new CarShowroom();
DisplayTalk(toyotaShowroom, 12000, "Tom");
DisplayTalk(toyotaShowroom, 14000, "Camille");
DisplayTalk(toyotaShowroom, 17000, "Ros");
DisplayTalk(toyotaShowroom, 22000, "Jack");
DisplayTalk(toyotaShowroom, 100000, "Bill");
}
}
}

OUTPUT:

Tom visit the toyota car showroom

Dealer: Hello Tom. How many Dollars would you spend?

Tom: Hi, I have for it 12000 dollars. What can you offer to me ?

Dealer: Ohw, I can offer you: No Car. It is the best I can offer to you.

Camille visit the toyota car showroom

Dealer: Hello Camille. How many Dollars would you spend?

Camille: Hi, I have for it 14000 dollars. What can you offer to me ?

Dealer: Ohw, I can offer you: Toyota Yaris. It is the best I can offer to you.

Ros visit the toyota car showroom

Dealer: Hello Ros. How many Dollars would you spend?

Ros: Hi, I have for it 17000 dollars. What can you offer to me ?

Dealer: Ohw, I can offer you: Toyota Corolla. It is the best I can offer to you.

Jack visit the toyota car showroom

Dealer: Hello Jack. How many Dollars would you spend?

Jack: Hi, I have for it 22000 dollars. What can you offer to me ?

Dealer: Ohw, I can offer you: Toyota RAV4. It is the best I can offer to you.

Bill visit the toyota car showroom

Dealer: Hello Bill. How many Dollars would you spend?

Bill: Hi, I have for it 100000 dollars. What can you offer to me ?

Dealer: Ohw, I can offer you: Toyota Land Cruiser. It is the best I can offer to you.

Use when

  • Flexibility is important.
  • Objects can be extended in subclasses.
  • There is a specific reason why one subclass would be chosen over another – this logic forms part of the Factory Method.
  • A client delegates responsibilities to subclasses in parallel hierarchies.

Consider using instead

  • The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).
  • The Prototype pattern to store a set of objects to clone from abstract factory.

Bibliography

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s