Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Could anyone please verify my code which is following abstract factory design pattern?

Below is my code,

Abstract product:
C#
namespace AbstractFactorypattern
{
    interface IPackage
    {
        string Package();
    }

    interface IDesignation
    {
        string Designation();
    }

    interface ILocation
    {
        string Location();

    }
}



Concreate product:
C#
namespace AbstractFactorypattern
{
    //TCS
    class TCSPcakage:IPackage
    {
        public string Package()
        {
            return "TCS: 10 lakhs";
        }
    }
    class TCSDesignation : IDesignation
    {
        public string Designation()
        {
            return "TCS: .net Developer";
        }
    }
    class TCSLocation : ILocation
    {
        public string Location()
        {
            return "TCS: UK";
        }
    }


    //WIPRO


    class WIPROPcakage : IPackage
    {
        public string Package()
        {
            return "WIPRO: 10 lakhs";
        }
    }
    class WIPRODesignation : IDesignation
    {
        public string Designation()
        {
            return "WIPRO: .net Developer";
        }
    }
    class WIPROLocation : ILocation
    {
        public string Location()
        {
            return "WIPRO: UK";
        }
    }

    //INFO

    class INFOPcakage : IPackage
    {
        public string Package()
        {
            return "INFO: 10 lakhs";
        }
    }
    class INFODesignation : IDesignation
    {
        public string Designation()
        {
            return "INFO: .net Developer";
        }
    }
    class INFOLocation : ILocation
    {
        public string Location()
        {
            return "INFO: UK";
        }
    }
}



Abstract factory:

C#
namespace AbstractFactorypattern
{
    interface IAbstractFactory
    {
        IPackage GetPackage();
        IDesignation GetDesignation();
        ILocation GetLocation();
    }
}



Concrete factory

C#
namespace AbstractFactorypattern
{
    class ConcreateFactoryTCS:IAbstractFactory
    {
         public IPackage GetPackage()
         {
             return new TCSPcakage();

         }

        public IDesignation GetDesignation()
        {
            return  new TCSDesignation();
        }

        public ILocation GetLocation()
        {
            return new TCSLocation();
        }

    }

    //WIPRO
    class ConcreateFactoryWIPRO : IAbstractFactory
    {
        public IPackage GetPackage()
        {
            return new WIPROPcakage();

        }

        public IDesignation GetDesignation()
        {
            return new WIPRODesignation();
        }

        public ILocation GetLocation()
        {
            return new WIPROLocation();
        }

    }

    //INFO

    class ConcreateFactoryINFO : IAbstractFactory
    {
        public IPackage GetPackage()
        {
            return new INFOPcakage();

        }

        public IDesignation GetDesignation()
        {
            return new INFODesignation();
        }

        public ILocation GetLocation()
        {
            return new INFOLocation();
        }

    }
}


Client

C#
static void Main(string[] args)
       {
           IAbstractFactory objiabs = null;
           Console.WriteLine("Enter Company");
           string companyname = Console.ReadLine();

           if (companyname != null)
               switch (companyname)
               {
                   case "tcs":
                       objiabs=new ConcreateFactoryTCS();
                       break;
                   case "wipro":
                       objiabs = new ConcreateFactoryWIPRO();
                       break;
                   case  "info":
                       objiabs =new ConcreateFactoryINFO();
                       break;
               }
           Console.WriteLine(objiabs.GetPackage().Package());
           Console.WriteLine(objiabs.GetDesignation().Designation());
           Console.WriteLine(objiabs.GetLocation().Location());



           Console.Read();
       }


Is this correct factory pattern ?
Posted
Updated 12-Feb-15 1:56am
v2
Comments
John C Rayan 12-Feb-15 8:17am    
As far as I can see , I would say 'yes'
CHill60 12-Feb-15 8:19am    
Can't see much wrong with it - try running it and see if it works! :)
Am Gayathri 12-Feb-15 8:34am    
Its working.. :)
Sinisa Hajnal 12-Feb-15 8:43am    
I'd say yes. You should maybe consider having single class Package or Product or something that has properties Package, Designation and Location.

Also, instead of having a switch in main function, create separate FactoryProvider class which would be called with FactoryProvider.GetFactory (ConcreteFactoryTypeEnum.WiPro). GetFactory would hold switch and return IFactory
Am Gayathri 12-Feb-15 9:13am    
Thankss..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900