Question

Code in C#

    class Program
    {
        static void Main(string[] args)
        {
            var benzCar = new CarClient(new BenzCarFactory());
            var bmwCar = new CarClient(new BmwCarFactory());
            benzCar.BuildCar();
            bmwCar.BuildCar();
        }
    }

    public interface Wheel { }
    public interface Door { }
    public interface Body { }

    public class BenzWheel : Wheel { }
    public class BmwWheel : Wheel { }
    public class HondaWheel : Wheel { }

    public class BenzDoor : Door { }
    public class BmwDoor : Door { }
    public class HondaDoor : Door { }

    public class BenzBody : Body { }
    public class BmwBody : Body { }
    public class HondaBody : Body { }

    public interface CarFactory
    {
        Body BuildBody();
        Wheel BuildWheel();
        Door BuildDoor();       
    }

    public class BenzCarFactory: CarFactory {
        public Body BuildBody() { return new BenzBody(); }
        public Wheel BuildWheel() { return new BenzWheel(); }
        public Door BuildDoor() { return new BenzDoor(); }
    }
    public class BmwCarFactory : CarFactory
    {
        public Body BuildBody() { return new BmwBody(); }
        public Wheel BuildWheel() { return new BmwWheel(); }
        public Door BuildDoor() { return new BmwDoor(); }
    }

    public class HondaCarFactory : CarFactory
    {
        public Body BuildBody() { return new HondaBody(); }
        public Wheel BuildWheel() { return new HondaWheel(); }
        public Door BuildDoor() { return new HondaDoor(); }
    }

    public class CarClient
    {
        private CarFactory _carFactory;
        public CarClient(CarFactory factory)
        {
            _carFactory = factory;
        }

        public void BuildCar()
        {
            _carFactory.BuildBody();
            _carFactory.BuildDoor();
            _carFactory.BuildWheel();
            // Remaining Business logic to assemble the parts and build a car.
        }       
    } 
  1. Is this a valid Abstract Factory?
  2. Do you think a different design pattern will be better than this?
  3. I added only three parts for simplicity. When we have few other parts, shall we combine 'Builder' pattern with Abstract Factory to build Car object?
Was it helpful?

Solution

It certainly seems to be a valid Abstract Factory Pattern -- you're making sure that only certain brands of parts go with certain brands of cars.

However, I have to take some umbrage with your class design.

  • You have a CarFactory that doesn't actually produce a car?
  • But you have a car client that does?
  • Also, interfaces should be for classes that are functionally divergent in behavior across implementations.

Wheels/Doors are functionally no different from one car to another. The same exact wheel/tire can and is used across dozens of car model lines. Wheels have properties but are functionally identical. BMW can't claim exclusivity on 17 inch wheels.

So, good job on your abstract factory implementation :) But keep working on your object design.

For a great example of the Abstract Factory Pattern have a look at how Java does it's UI Toolkit and changes UI themes in it's non-existent world of desktop apps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top