none
Need explanation about Abstract Factory method design pattern RRS feed

  • Question

  • In the below example (Ref:http://www.c-sharpcorner.com/uploadfile/kalisk/factory-method-design-pattern-using-C-Sharp/)

     

    I don't understand the phrase "Define an interface for creating an object"

    and what does it mean "but let subclasses decide which class to instantiate""

    in the above i dont understand subclasses decide which class to instanitate...

    some one please explain me that using the below code...in the below code which are the suclasses which decide which class to instanitate...

     


    Definition:

    "Define an interface for creating an object, but let subclasses decide which class to instantiate"

    C# Implementation of Factory method

    abstract class Factory 
        {
            public abstract Product GetProduct(); //Factory Method Declaration
       
    }

    class
    concreteFactoryforProcuct1 : Factory
        {
            public override Product GetProduct() //Factory Method Implementation
               
    {
                    return new Product1();
                }
        }

    class concreteFactoryforProcuct2 : Factory
        {
            public override Product GetProduct() //Factory Method Implementation
               
    {
                    return new Product2();
                }
        }

    interface
    Product
        {
            void GetDetails();
        }

    class
    Product1 : Product
        {
            public void GetDetails()
            {
                Console.WriteLine("Product1 Details are Called");
            }
        }

    class
    Product2 : Product
        {
            public void GetDetails()
            {
                Console.WriteLine("Product2 Details are called");
            }
        }

    protected void Page_Load(object sender, EventArgs e)
    {

        Factory[] objFactories = new Factory[2];
        objFactories[0] = new concreteFactoryforProcuct1();
        objFactories[1] = new concreteFactoryforProcuct2();
        foreach (Factory objFactory in objFactories)
      
     {
            Product objProduct = objFactory.GetProduct();
            objProduct.GetDetails();
        }
    }



    ilikemicrosoft
    Tuesday, January 31, 2012 3:09 PM

All replies

  • concreteFactoryforProcuct1  and concreteFactoryforProcuct2 are the subclasses (subclasses of Factory) that decide to instantiate Product1 and Product2 respectively in their GetProduct() methods.

    The abstract factory is a design pattern that helps to decouples creation of a type from the consumer of a type.

    In your code the consumer is the class that implements “Page_Load(object sender,EventArgs e)”.

    By calling "Product objProduct = objFactory.GetProduct();" instead of "Product1 objProduct = new Product1;" decouples the UI class (that implements Page_Load) from knowing what product to create and how to create it. This gives you flexibility to make changes in one class without having to deal with a ripple effect in their dependencies.

    Wednesday, February 1, 2012 4:11 PM