Design patterns in C# – part 12 (Builder pattern)

Role

The Builder pattern separates the specification of a complex object from its actual construction.

Design

BuilderPattern

Implementation

  • Client -> TestBuilderPattern
  • Director -> CarDirector
  • IBuilder -> ICarBuilder
  • Product -> Car
  • Builder -> LuxuryCarBuilder, FamilyCarBuilder, SportCarBuilder, EconomyCarBuilder
namespace BuilderPattern
{
    public enum CarBody
    {
        Economy,
        Sport,
        Luxury
    }

    public enum CarChassis
    {
        Low,
        Normal
    }

    public enum CarInterior
    {
        Economy,
        Standard,
        Luxury
    }

    public enum CarEngine
    {
        Economy,
        Turbo,
        Standard
    }

    public interface ICar
    {
        CarBody Body { get; set; }
        CarChassis Chassis { get; set; }
        CarInterior Interior { get; set; }
        CarEngine Engine { get; set; }
        CarType Type { get; set; }
    }

    public class Car : ICar
    {
        #region ICar Members

        CarBody ICar.Body { get; set; }

        CarChassis ICar.Chassis { get; set; }

        CarInterior ICar.Interior { get; set; }

        CarEngine ICar.Engine { get; set; }

        CarType ICar.Type { get; set; }

        #endregion
    }
}
namespace BuilderPattern
{
    public interface ICarBuilder
    {
        void BuildBody();
        void BuildChassis();
        void BuildInterior();
        void BuildEngine();
        ICar GetResult();
    }

    public abstract class CarPrototype
    {
        protected readonly ICar _car = new Car();
        public abstract CarType Type { get; }

        public ICar GetResult()
        {
            return this._car;
        }
    }

    public enum CarType
    {
        Sport,
        Family,
        Luxury,
        Economy
    }

    public class SportCarBuilder : CarPrototype, ICarBuilder
    {
        public override CarType Type
        {
            get { return CarType.Sport; }
        }

        #region ICarBuilder Members

        public void BuildBody()
        {
            _car.Body = CarBody.Sport;
        }

        public void BuildChassis()
        {
            _car.Chassis = CarChassis.Low;
        }

        public void BuildInterior()
        {
            _car.Interior = CarInterior.Standard;
        }

        public void BuildEngine()
        {
            _car.Engine = CarEngine.Turbo;
        }

        #endregion
    }

    public class LuxuryCarBuilder : CarPrototype, ICarBuilder
    {
        public override CarType Type
        {
            get { return CarType.Luxury; }
        }

        #region ICarBuilder Members

        public void BuildBody()
        {
            _car.Body = CarBody.Luxury;
        }

        public void BuildChassis()
        {
            _car.Chassis = CarChassis.Normal;
        }

        public void BuildInterior()
        {
            _car.Interior = CarInterior.Luxury;
        }

        public void BuildEngine()
        {
            _car.Engine = CarEngine.Standard;
        }

        #endregion
    }

    public class FamilyCarBuilder : CarPrototype, ICarBuilder
    {
        public override CarType Type
        {
            get { return CarType.Family; }
        }

        #region ICarBuilder Members

        public void BuildBody()
        {
            _car.Body = CarBody.Economy;
        }

        public void BuildChassis()
        {
            _car.Chassis = CarChassis.Normal;
        }

        public void BuildInterior()
        {
            _car.Interior = CarInterior.Standard;
        }

        public void BuildEngine()
        {
            _car.Engine = CarEngine.Standard;
        }

        #endregion
    }

    public class EconomyCarBuilder : CarPrototype, ICarBuilder
    {
        public override CarType Type
        {
            get { return CarType.Economy; }
        }

        #region ICarBuilder Members

        public void BuildBody()
        {
            _car.Body = CarBody.Economy;
        }

        public void BuildChassis()
        {
            _car.Chassis = CarChassis.Normal;
        }

        public void BuildInterior()
        {
            _car.Interior = CarInterior.Economy;
        }

        public void BuildEngine()
        {
            _car.Engine = CarEngine.Economy;
        }

        #endregion
    }
}
using System;

namespace BuilderPattern
{
    public class CarDirector
    {
        public void ConstructCar(ICarBuilder builder)
        {
            builder.BuildBody();
            builder.BuildChassis();
            builder.BuildInterior();
            builder.BuildEngine();
        }
    }

    public class TestBuilderPattern
    {
        public static void DisplayCarInfo(ICar car)
        {
            Console.WriteLine("\n\n--------------------");
            Console.WriteLine("Hello in Car Info !!\n");
            Console.WriteLine("Kind of car: \t{0}", car.Type);
            Console.WriteLine("Body: \t\t{0}", car.Body);
            Console.WriteLine("Chasis: \t{0}", car.Chassis);
            Console.WriteLine("Interior: \t{0}", car.Interior);
            Console.WriteLine("Engine: \t{0}", car.Engine);
        }

        public static void Main(string[] args)
        {
            var director = new CarDirector();

            ICarBuilder luxuryCarBuilder = new LuxuryCarBuilder();
            ICarBuilder sportCarBuilder = new SportCarBuilder();
            ICarBuilder familyCarBuilder = new FamilyCarBuilder();
            ICarBuilder economyCarBuilder = new EconomyCarBuilder();

            director.ConstructCar(luxuryCarBuilder);
            director.ConstructCar(familyCarBuilder);
            director.ConstructCar(sportCarBuilder);
            director.ConstructCar(economyCarBuilder);

            DisplayCarInfo(luxuryCarBuilder.GetResult());
            DisplayCarInfo(familyCarBuilder.GetResult());
            DisplayCarInfo(sportCarBuilder.GetResult());
            DisplayCarInfo(economyCarBuilder.GetResult());
        }
    }
}

OUTPUT:

——————–
Hello in Car Info !!
Kind of car:    Sport
Body:           Luxury
Chasis:         Normal
Interior:       Luxury
Engine:         Standard
——————–
Hello in Car Info !!
Kind of car:    Sport
Body:           Economy
Chasis:         Normal
Interior:       Standard
Engine:         Standard
——————–
Hello in Car Info !!
Kind of car:    Sport
Body:           Sport
Chasis:         Low
Interior:       Standard
Engine:         Turbo
——————–
Hello in Car Info !!
Kind of car:    Sport
Body:           Economy
Chasis:         Normal
Interior:       Economy
Engine:         Economy

Use when

  • The algorithm for creating parts is independent from the parts themselves.
  • The object to be assembled might have different representations.
  • You need fine control over the construction process.

Bibliography

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s