Many times it seems like factory and factory method pattern are doing the same job. But the evil is hidden in details. Let me show you what I am talking about.
Car Factory Method Story
Consider a factory which is manufacturing cars. It builds different parts of the car such as Door, Steering, Body and Engine etc. Then it puts these parts on to the chassis.
Car factory method class looks like this
public abstract class CarFactory { protected abstract string BuildSteering(); protected abstract string BuildWheels(); protected abstract string BuildDoors(); public Car BuildCar() { var car = new Car(); car.Steering = BuildSteering(); Console.WriteLine("Steering is {0}",car.Steering); Console.WriteLine("Fit it with Bronze Bolts and Nuts on Chassis"); car.Wheels = BuildWheels(); Console.WriteLine("Wheels are {0}", car.Wheels); Console.WriteLine("Fit it with Bronze Bolts and Nuts on Chassis"); car.Doors = BuildDoors(); Console.WriteLine("Doors are {0}", car.Doors); Console.WriteLine("Fit it with Bronze Bolts and Nuts on Chassis"); return car; } } Note that for all cars the parts are connected to chassis in the same way. All parts will be placed and fastened with bolts and nuts.
Company decided to manufacture sports car which has alloy wheels, fiber doors and power steering. All parts are manufactured and sent to assembly line for adding the parts. Now in assembly line same procedure of fitting these parts are followed (which is fastening with bronze bolts and nuts)
So Sports car factory looks like
public class SportsCarFactory : CarFactory { protected override string BuildDoors() { return "Fiber Doors"; } protected override string BuildWheels() { return "Alloy wheels"; } protected override string BuildSteering() { return "Power Steering"; } } Competitor turned up 😦
Company opened many similar factories with same assembly operations until one day competitor turned up.
Just like competitor, company decided to manufacture the sports cars and commercial cars differently. They feel instead of using bronze bolts and nuts for sports cars, they can use fiber bolts to give speed advantage. Yeah.. sure.. why not.. But what about Commercial cars? They are built with metal body, and they can not take fiber bolts. Errr… Problem 😦
Why problem? Because you can customize the parts, but can not customize the assembly operations.
Into New Business with Abstract Factory 🙂
Company realized they need two different assembly operations for manufacturing cars. But they are still building cars. Now car factory is just an abstraction. It doesn’t dictate any assembly operations.
public abstract class CarFactory { protected abstract string BuildSteering(); protected abstract string BuildWheels(); protected abstract string BuildDoors(); public abstract Car BuildCar(); } Now the Sports car factory and commercial car factory look like
public class SportsCarFactory : CarFactory { protected override string BuildDoors() { return "Fiber Doors"; } protected override string BuildWheels() { return "Alloy wheels"; } protected override string BuildSteering() { return "Power Steering"; } public override Car BuildCar() { var car = new Car(); car.Steering = BuildSteering(); Console.WriteLine("Steering is {0}", car.Steering); Console.WriteLine("Fit it with fiber Bolts and Nuts on Chassis"); car.Wheels = BuildWheels(); Console.WriteLine("Wheels are {0}", car.Wheels); Console.WriteLine("Fit it with fiber Bolts and Nuts on Chassis"); car.Doors = BuildDoors(); Console.WriteLine("Doors are {0}", car.Doors); Console.WriteLine("Fit it with fiber Bolts and Nuts on Chassis"); return car; } } public class CommercialCarFactory : CarFactory { protected override string BuildDoors() { return "Metal Doors"; } protected override string BuildWheels() { return "Alloy wheels"; } protected override string BuildSteering() { return "Power Steering"; } public override Car BuildCar() { var car = new Car(); car.Steering = BuildSteering(); Console.WriteLine("Steering is {0}", car.Steering); Console.WriteLine("Fit it with bronze Bolts and Nuts on Chassis"); car.Wheels = BuildWheels(); Console.WriteLine("Wheels are {0}", car.Wheels); Console.WriteLine("Fit it with bronze Bolts and Nuts on Chassis"); car.Doors = BuildDoors(); Console.WriteLine("Doors are {0}", car.Doors); Console.WriteLine("Fit it with bronze Bolts and Nuts on Chassis"); return car; } } We turned away from factory method to abstract factory.
Let me know what you guys think.. 🙂


Try the code here..
