none
Interesting Abstract Factory Pattern by Judith Bishop RRS feed

  • General discussion

  • I thought this was a very interesting example of the Abstract Factory Pattern. Thought some of you would appreciate this example:

    using System;

    namespace AbstractFactoryPattern
    {
        static class Program
        {
            static void Main()
            {
                // Call Client twice
                new Client<Poochy>().ClientMain();
                new Client<Gucci>().ClientMain();
                new Client<Groundcover>().ClientMain();
                Console.ReadLine();
            }
        }

        interface IFactory<Brand>
          where Brand : IBrand
        {
            IBag CreateBag();
            IShoes CreateShoes();
        }

        // Conctete Factories (both in the same one)
        class Factory<Brand> : IFactory<Brand>
          where Brand : IBrand, new()
        {
            public IBag CreateBag()
            {
                return new Bag<Brand>();
            }

            public IShoes CreateShoes()
            {
                return new Shoes<Brand>();
            }
        }

        // Product 1
        interface IBag
        {
            string Material { get; }
        }

        // Product 2
        interface IShoes
        {
            int Price { get; }
        }

        // Concrete Product 1
        class Bag<Brand> : IBag
          where Brand : IBrand, new()
        {
            private Brand myBrand;
            public Bag()
            {
                myBrand = new Brand();
            }

            public string Material { get { return myBrand.Material; } }
        }

        // Concrete Product 2
        class Shoes<Brand> : IShoes
          where Brand : IBrand, new()
        {

            private Brand myBrand;

            public Shoes()
            {
                myBrand = new Brand();
            }

            public int Price { get { return myBrand.Price; } }
        }

        interface IBrand
        {
            int Price { get; }
            string Material { get; }
        }

        class Gucci : IBrand
        {
            public int Price { get { return 1000; } }
            public string Material { get { return "Crocodile skin"; } }
        }

        class Poochy : IBrand
        {
            public int Price { get { return new Gucci().Price / 3; } }
            public string Material { get { return "Plastic"; } }
        }

        class Groundcover : IBrand
        {
            public int Price { get { return 2000; } }
            public string Material { get { return "South african leather"; } }
        }

        class Client<Brand>
          where Brand : IBrand, new()
        {
            public void ClientMain()
            {
                IFactory<Brand> factory = new Factory<Brand>();

                IBag bag = factory.CreateBag();
                IShoes shoes = factory.CreateShoes();

                Console.WriteLine("I bought a Bag which is made from {0}", bag.Material);
                Console.WriteLine("I bought some shoes which cost {0}", shoes.Price);
            }
        }

      
    }

     


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Edited by JohnGrove Friday, May 1, 2009 9:34 PM
    Friday, May 1, 2009 9:30 PM

All replies

  • Was this out of C# 3 Design Patterns, or from an online source?

    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, May 1, 2009 11:29 PM
    Moderator
  • Yea that was from C# 3.0, I am a little slow even know it is simplistic I had to look at it for 15 minutes before I completely understood it. I thought it was a nice example. Hey, nice to finally see a pic Reed.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Saturday, May 2, 2009 1:22 AM