none
The difference between Factory method design pattern and Abstract Factory RRS feed

  • Question

  • Hi,

    They are very similar from UML. I want to know the differences between them and how to use them in actual world. 

    I found there is only one instance in Factory method, but more than one in abstract Factory?  please give me some samples to clarfiy me, thanks. I don't like links.


    I am fish.
    Wednesday, August 24, 2011 3:25 PM

Answers

  • I've already gave you. You need to create ICar and IWheel concrete implementations to work with them. You have 2 abstract factories: FerrariFactory and BMWFactory. Also you have 4 classes in your system: {FerrariCar, FerrariWheel}, {BMWCar, BMWWheel} (two interoperable families of classes). Why interoperable? Because BMWCar is better to run on BMWWheels, but not on Ferrarie's wheels. Maybee it could run on Ferrarie's wheel implementation (in case if we respect Liskvo's principle), but I have doubths (what if BMW's wheels are larger on their's size? Or have some conditions that are not compatible with Ferrarie's?) In any case you could create 2 different classes with one factory method in each one, or you could create one abstract factory which defines 2 factory-methods (CreateCar/Wheel) which work in one "family-context" (bmw family/ferrari family). Abstract factory protects you of using BMWCar on FerrariWheels, as you could do after creating them with FactoryMethods defined in different classes (SomeWheelFactory/SomeCarFactory).

    The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme. (c)




    • Marked as answer by Fly_fish Wednesday, August 31, 2011 10:11 AM
    Thursday, August 25, 2011 10:43 AM
  • Factory method simply instantiates some object, it can do that polimiorphically. You can replace your factory with another one to instantiate different object of the same interface. Abstract factory can produce families of interoperable objects (that fit each other). For example, I need to create a car and wheel. One abstract factory could return a BMW + BMW Wheel (this wheel could be used with BMW, but not with another cars), another - Ferarri + Ferrarri wheel.

     

    public interface ICarFactory
    
    {
    
      public ICar CreateCar();
    
      public IWheel CreateWheel();
    
    }
    
    
    public class BMWFactory : ICarFactory
    
    {
    
      public ICar CreateCar()
    
      {
    
        return new BMW();
    
      }
    
    
      public IWheel CreateWheel()
    
      {
    
         return new BMWWheel();
    
      }
    
    }
    
    
    public class FerarrFactory : ICarFactory
    
    {
    
      //...
    
    }
    

    So, abstract factory produces a families of objects that could fit each other (I mean compatibility between concrete classes that belong to one family).

    for more details see discussion at http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod

    • Proposed as answer by Leonid Ganeline Thursday, August 25, 2011 5:46 PM
    • Marked as answer by Fly_fish Wednesday, August 31, 2011 10:12 AM
    Thursday, August 25, 2011 8:12 AM

All replies

  • The factory pattern is pretty simple in concept. It's basically a method which creates and returns objects for you, which allows loose binding. A lot of the time the factory method takes a parameter which describes which type of object to create. For example:

    enum RoomType
    {
        Bedroom,
        Bathroom,
        Garage,
        Kitchen
    }
    
    public static class RoomFactory
    {
        public static IRoom CreateRoom(RoomType type)
        {
            switch (type)
            {
                case (RoomType.Bedroom):
                    return new Bedroom();
                case (RoomType.Bathroom):
                    return new Bathroom();
                case (RoomType.Garage)
                    return new Garage();
                case RoomType.Kitchen:
                    return new Kitchen();
                default:
                    return null;
            }
        }
    }
    


     

    Assuming that all of the rooms derive from IRoom...you get the idea.

     

    Abstract Factory is one level more complicated. It is sort of like a factory of factories. To stick with the example above, the Factory pattern may be used to create different types of rooms without having to care what the various rooms are. The Abstract Factory pattern may be used to create different types of houses, each of which contains different types of rooms. In this case, I would do something like this:

    public interface IHomeFactory
    {
        IEnumerable<IRoom> CreateRooms();
    }
    
    public static class CondoFactory : IHomeFactory
    {
        public static IEnumerable<IRoom> CreateRooms()
        {
            List<IRoom> roomList = new List<IRoom>();
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bedroom));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bedroom));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Kitchen));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bathroom));
            return roomList.AsReadOnly();
        }
    }
    
    public static class HouseFactory : IHomeFactory
    {
        public static IEnumerable<IRoom> CreateRooms()
        {
            List<IRoom> roomList = new List<IRoom>();
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bedroom));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bedroom));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bedroom));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Kitchen));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Bathroom));
            roomList.Add(RoomFactory.CreateRoom(RoomType.Garage));
            return roomList.AsReadOnly();
        }
    }
    
    enum HomeType
    {
        House,
        Condo
    }
    
    public static class HomeFactory
    {
        public static IEnumerable<IRoom> CreateRoomsForHome(HomeType type)
        {
            switch (type)
            {
                case HomeType.House:
                    return HouseFactory.CreateRooms();
                case HomeType.Condo:
                    return CondoFactory.CreateRooms();
            }
        }
    }
    



    Check out My Blog for tech news, development tips, and other information for geeks like me.
    Wednesday, August 24, 2011 4:13 PM
  • Hi,

    Thanks for  your reply, but the first sample is Simple Factory, the second sample is Factory method not abstract factory.

     


    I am fish.
    Thursday, August 25, 2011 2:55 AM
  • Factory method simply instantiates some object, it can do that polimiorphically. You can replace your factory with another one to instantiate different object of the same interface. Abstract factory can produce families of interoperable objects (that fit each other). For example, I need to create a car and wheel. One abstract factory could return a BMW + BMW Wheel (this wheel could be used with BMW, but not with another cars), another - Ferarri + Ferrarri wheel.

     

    public interface ICarFactory
    
    {
    
      public ICar CreateCar();
    
      public IWheel CreateWheel();
    
    }
    
    
    public class BMWFactory : ICarFactory
    
    {
    
      public ICar CreateCar()
    
      {
    
        return new BMW();
    
      }
    
    
      public IWheel CreateWheel()
    
      {
    
         return new BMWWheel();
    
      }
    
    }
    
    
    public class FerarrFactory : ICarFactory
    
    {
    
      //...
    
    }
    

    So, abstract factory produces a families of objects that could fit each other (I mean compatibility between concrete classes that belong to one family).

    for more details see discussion at http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod

    • Proposed as answer by Leonid Ganeline Thursday, August 25, 2011 5:46 PM
    • Marked as answer by Fly_fish Wednesday, August 31, 2011 10:12 AM
    Thursday, August 25, 2011 8:12 AM
  • Hi,

    Thanks.

    >>Abstract factory can produce families of interoperable objects

    This is the definition of the abstract Factory, I really want to know "families of interoperable objects ". this confused me for a long time. so i couldn't tell the really difference between them. If someone could give me the real world sample(projects), I will appreciate it.


    I am fish.
    Thursday, August 25, 2011 9:48 AM
  • I've already gave you. You need to create ICar and IWheel concrete implementations to work with them. You have 2 abstract factories: FerrariFactory and BMWFactory. Also you have 4 classes in your system: {FerrariCar, FerrariWheel}, {BMWCar, BMWWheel} (two interoperable families of classes). Why interoperable? Because BMWCar is better to run on BMWWheels, but not on Ferrarie's wheels. Maybee it could run on Ferrarie's wheel implementation (in case if we respect Liskvo's principle), but I have doubths (what if BMW's wheels are larger on their's size? Or have some conditions that are not compatible with Ferrarie's?) In any case you could create 2 different classes with one factory method in each one, or you could create one abstract factory which defines 2 factory-methods (CreateCar/Wheel) which work in one "family-context" (bmw family/ferrari family). Abstract factory protects you of using BMWCar on FerrariWheels, as you could do after creating them with FactoryMethods defined in different classes (SomeWheelFactory/SomeCarFactory).

    The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme. (c)




    • Marked as answer by Fly_fish Wednesday, August 31, 2011 10:11 AM
    Thursday, August 25, 2011 10:43 AM
  • Thanks very much, I think I got it now. thanks for your kindly reply.


    I am fish.
    Wednesday, August 31, 2011 10:11 AM