Quantcast
Channel: What are the differences between Abstract Factory and Factory design patterns? - Stack Overflow
Viewing all articles
Browse latest Browse all 22

Answer by Saurabh for What are the differences between Abstract Factory and Factory design patterns?

$
0
0

To make it very simple with minimum interface & please focus "//1":

class FactoryProgram    {        static void Main()        {            object myType = Program.MyFactory("byte");            Console.WriteLine(myType.GetType().Name);            myType = Program.MyFactory("float"); //3            Console.WriteLine(myType.GetType().Name);            Console.ReadKey();        }        static object MyFactory(string typeName)        {            object desiredType = null; //1            switch (typeName)            {                case "byte": desiredType = new System.Byte(); break; //2                case "long": desiredType = new System.Int64(); break;                case "float": desiredType = new System.Single(); break;                default: throw new System.NotImplementedException();            }            return desiredType;        }    }

Here important points: 1. Factory & AbstractFactory mechanisms must use inheritance (System.Object-> byte, float ...); so if you have inheritance in program then Factory(Abstract Factory would not be there most probably) is already there by design 2. Creator (MyFactory) knows about concrete type so returns concrete type object to caller(Main); In abstract factory return type would be an Interface.

interface IVehicle { string VehicleName { get; set; } }interface IVehicleFactory    {        IVehicle CreateSingleVehicle(string vehicleType);    }class HondaFactory : IVehicleFactory    {        public IVehicle CreateSingleVehicle(string vehicleType)        {            switch (vehicleType)            {                case "Sports": return new SportsBike();                case "Regular":return new RegularBike();                default: throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", vehicleType));            }        }    }class HeroFactory : IVehicleFactory    {        public IVehicle CreateSingleVehicle(string vehicleType)        {            switch (vehicleType)            {                case "Sports":  return new SportsBike();                case "Scooty": return new Scooty();                case "DarkHorse":return new DarkHorseBike();                default: throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", vehicleType));            }        }    }class RegularBike : IVehicle { public string VehicleName { get { return "Regular Bike- Name"; } set { VehicleName = value; } } }class SportsBike : IVehicle { public string VehicleName { get { return "Sports Bike- Name"; } set { VehicleName = value; } } }class RegularScooter : IVehicle { public string VehicleName { get { return "Regular Scooter- Name"; } set { VehicleName = value; } } }class Scooty : IVehicle { public string VehicleName { get { return "Scooty- Name"; } set { VehicleName = value; } } }class DarkHorseBike : IVehicle { public string VehicleName { get { return "DarkHorse Bike- Name"; } set { VehicleName = value; } } }class Program{    static void Main(string[] args)    {        IVehicleFactory honda = new HondaFactory(); //1        RegularBike hondaRegularBike = (RegularBike)honda.CreateSingleVehicle("Regular"); //2        SportsBike hondaSportsBike = (SportsBike)honda.CreateSingleVehicle("Sports");        Console.WriteLine("******* Honda **********"+hondaRegularBike.VehicleName+ hondaSportsBike.VehicleName);        IVehicleFactory hero = new HeroFactory();        DarkHorseBike heroDarkHorseBike = (DarkHorseBike)hero.CreateSingleVehicle("DarkHorse");        SportsBike heroSportsBike = (SportsBike)hero.CreateSingleVehicle("Sports");        Scooty heroScooty = (Scooty)hero.CreateSingleVehicle("Scooty");        Console.WriteLine("******* Hero **********"+heroDarkHorseBike.VehicleName + heroScooty.VehicleName+ heroSportsBike.VehicleName);        Console.ReadKey();    }}

Important points: 1. Requirement: Honda would create "Regular", "Sports" but Hero would create "DarkHorse", "Sports" and "Scooty". 2. why two interfaces? One for manufacturer type(IVehicleFactory) and another for product factory(IVehicle); other way to understand 2 interfaces is abstract factory is all about creating related objects 2. The catch is the IVehicleFactory's children returning and IVehicle(instead of concrete in factory); so I get parent variable(IVehicle); then I create actual concrete type by calling CreateSingleVehicle and then casting parent object to actual child object. What would happen if I do RegularBike heroRegularBike = (RegularBike)hero.CreateSingleVehicle("Regular");; you will get ApplicationException and that's why we need generic abstract factory which I would explain if required. Hope it helps from beginner to intermediate audience.


Viewing all articles
Browse latest Browse all 22

Trending Articles