Factory pattern in C#

using System;

namespace CSharpBasics
{
    //Factory Pattern

    //*************FROM MSDN************
    //http://msdn.microsoft.com/en-us/library/ms954600.aspx
    //primary actors, a client, a factory, and a product. 
    //The client is an object that requires an instance of another object 
    //(the product) for some purpose. Rather than creating the product instance 
    //directly, the client delegates this responsibility to the factory. 
    //Once invoked, the factory creates a new instance of the product, 
    //passing it back to the client. Put simply, the client uses the factory 
    //to create an instance of the product. 

    //Client ---------Uses---------> Factory ----------Creates----------> Product

    //The factory completely abstracts the creation and initialization 
    //of the product from the client.

    //This indirection enables the client to focus on its discrete role 
    //in the application without concerning itself with the details of 
    //how the product is created (e.g. new constructor, use of an activator, 
    //custom pooling, etc.), .

    //Thus, as the product implementation changes over time, 
    //the client remains unchanged.

    //the most important aspect of this pattern is the fact that the client 
    //is abstracted from both the type of product and the type of factory 
    //used to create the product.

    //Difference between abstract factory and factory pattern is :
    //In Abstract Factory we do not have to initialize the object using
    //Base class implementation, instead this is handled by the AbstractFactory class.
    //However in Factory pattern we have to explicitly initialize the object using base class

    //Base (server) abstract, we can also use interfaces here
    abstract class FactoryBaseCls
    {
        public abstract string MyImplementation();
    }

    //Factory (can also say adaptor) abstract, we can also use interfaces here
    abstract class FactoryCls
    {
        public abstract FactoryBaseCls getFactoryBaseCls();
    }

    //AbstractFactory (can also say adaptor) abstract, we can also use interfaces here
    abstract class AbstractFactoryCls
    {
        public abstract FactoryBaseCls getFactoryBaseCls(int i);
    }


    //concrete class (server) implementation
    class FactoryBase : FactoryBaseCls
    {
        private DateTime dt;
        public FactoryBase()
        {
            dt = DateTime.Now;
        }

        public override string ToString()
        {
            return dt.ToShortDateString();
        }

        public override string MyImplementation()
        {
            return "This is implementation of abstract class (Factory Pattern).";
        }
    }

    //concrete class (server) implementation
    class FactoryBase1 : FactoryBaseCls
    {
        private int dt;
        public FactoryBase1()
        {
            dt = 23;
        }

        public override string ToString()
        {
            return dt.ToString();
        }

        public override string MyImplementation()
        {
            return "This is implementation of abstract class*1* (Factory Pattern).";
        }
    }

    //factory class implementation
    class Factory : FactoryCls
    {
        public override FactoryBaseCls getFactoryBaseCls()
        {
            return new FactoryBase();
        }
    }
    //another factory class implementation
    class Factory1 : FactoryCls
    {
        public override FactoryBaseCls getFactoryBaseCls()
        {
            return new FactoryBase1();
        }
    }

    //AbstractFactory Factory Class
    class AbstractFactory : AbstractFactoryCls
    {
        public override FactoryBaseCls getFactoryBaseCls(int i)
        {
            if (i == 0)
                return new FactoryBase();
            else
                return new FactoryBase1();
        }
    }

    //Client for Factory pattern
    class Client
    {
        public Client() { }

        public void useServerObjectUsingFactory(int i)
        {
            FactoryCls f;
            if (i == 0)
                f = new Factory();
            else
                f = new Factory1();
            Console.WriteLine("Method MyImplementation called : " + f.getFactoryBaseCls().MyImplementation());
            Console.WriteLine("ToString called : " + f.getFactoryBaseCls().ToString());
        }
    }

    //Client for Abstract Factory pattern
    class ClientForAbstract
    {
        public ClientForAbstract() { }

        public void useServerObjectUsingFactory(AbstractFactory f)
        {
            FactoryBaseCls fc = f.getFactoryBaseCls(0);
            Console.WriteLine("Method MyImplementation called : " + fc.MyImplementation());
            Console.WriteLine("ToString called : " + fc.ToString());
            fc = f.getFactoryBaseCls(1);
            Console.WriteLine("Method MyImplementation called : " + fc.MyImplementation());
            Console.WriteLine("ToString called : " + fc.ToString());
        }
    }
}

Add Comments :

Login

Register