Example: Abstract Factory Pattern in C#

Pattern Type: Creational

The abstract factory pattern is a way of encapsulating a group of factories which have a common theme. It utilizes generic interfaces to allow it to create concrete objects which are part of the common theme. Using this pattern you can interchange concrete classes without changing the code that uses them. The example that I’m going to give is car manufacturer’s; Three nationalities will be used as the example (American, German and Japanese).

Below is a simple example of this kind of implementation. You should be able to copy and paste this directly into a new C# console application. You will probably be able to understand this more by putting the code directly into Visual Studio, putting breakpoints into the code, then follow it through to understand how its holding together. I find that if you can see the pattern working it makes more sense that hundreds of lines of text explaining the hows’ and why’s.

The source code should be quite self explanatory.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbstractFactoryPattern{
    class Program{
        static void Main(string[] args){

            GetMeACar myCar = new GetMeACar();
            myCar.Cars(Nationality.German, CarManufacturer.BMW);
            myCar.Cars(Nationality.German, CarManufacturer.Mercedes);
            myCar.Cars(Nationality.Japanese, CarManufacturer.Kia);
            myCar.Cars(Nationality.Japanese, CarManufacturer.Ford);
            Console.ReadKey();
        }
    }

    public enum CarManufacturer{
        BMW, Mercedes, Ford, Nissan, Kia
    }
    public enum Nationality{
        American, German, Japanese
    }
    public interface IManufacture{
        Car BuildCar(CarManufacturer type);
    }
    public interface Car{
        void Make();
    }


    public class BMWCars : Car { public void Make() { Console.WriteLine("Made a new BMW"); } }
    public class MercedesCars : Car { public void Make() { Console.WriteLine("Made a new Mercedes"); } }
    public class FordCars : Car { public void Make() { Console.WriteLine("Made a new Ford"); } }
    public class NissanCars : Car { public void Make() { Console.WriteLine("Made a new Nissan"); } }
    public class KiaCars : Car { public void Make() { Console.WriteLine("Made a new Kia"); } }

    public class GetMeACar{
        public void Cars(Nationality wherefrom, CarManufacturer carType){
            Car tmpCar = null;
            switch (wherefrom){
                case Nationality.American: {
                    AmericanCars MyAmericanCar = new AmericanCars();
                    tmpCar = MyAmericanCar.BuildCar(carType);
                    break;
                }
                case Nationality.German:{
                        GermanCars MyGermanCar = new GermanCars();
                        tmpCar = MyGermanCar.BuildCar(carType);
                        break;
                }
                case Nationality.Japanese:{
                        JapaneseCars MyJapaneseCar = new JapaneseCars();
                        tmpCar = MyJapaneseCar.BuildCar(carType);
                        break;
                }
            }
            if (tmpCar != null){
                tmpCar.Make();
            }
            else{
                Console.WriteLine("Unable to make car {0} as this is not made by a {1} manufacturer ",carType.ToString(), wherefrom.ToString());
            }
        }
    }

    public class GermanCars : IManufacture{
        public Car BuildCar(CarManufacturer type){
            Car tmpCar = null;
            switch (type){
                case CarManufacturer.BMW : {
                    tmpCar = new BMWCars();
                    break;
                }
                case CarManufacturer.Mercedes:{
                    tmpCar = new MercedesCars();
                    break;
                }
            }
            return tmpCar;
        }
    }

    public class AmericanCars : IManufacture{
        public Car BuildCar(CarManufacturer type){
            Car tmpCar = null;
            switch (type){
                case CarManufacturer.Ford:{
                    tmpCar = new FordCars();
                    break;
                }
            }
            return tmpCar;
        }
    }

    public class JapaneseCars : IManufacture{
        public Car BuildCar(CarManufacturer type){
            Car tmpCar = null;
            switch (type){
                case CarManufacturer.Nissan:
                    {
                    tmpCar = new NissanCars();
                    break;
                }
                case CarManufacturer.Kia:{
                    tmpCar = new KiaCars();
                    break;
                }
            }
            return tmpCar;
        }
    }
}

15 Comments

  1. Hi,

    The exmaple that i gave above could quite easily be converted from the switch statements to using the Dictionary<> lookup idea that i demonstrate in another article on my site.

    Roger.

  2. , an innovative, young and promising company that was founded in 2004
    by two creative and enthusiastic young programmers.
    The gameplay is almost exactly as it was in the PC game, so you know that the mechanics behind the game are sound.
    It uses the Unreal game engine which makes the game physics amongst the best
    not only among Android RPG games but all Android games.

  3. I have checked your site and i’ve found some duplicate content, that’s
    why you don’t rank high in google’s search results, but there is a
    tool that can help you to create 100% unique content, search for; Boorfe’s tips unlimited content

Comments are closed.