BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 1.1+

Builder Design Pattern

The builder pattern is a design pattern that allows for the step-by-step creation of complex objects using the correct sequence of actions. The construction is controlled by a director object that only needs to know the type of object it is to create.

Example Builder

To show a real-world, but very simplistic, example of the use of a design pattern, we will create two builders that generate the fast food restaurant meals described earlier in this article. Each builder will create a meal with a named burger, side order, drink, special offer item and a price. In a real application, this process would probably be more complex and may require that the items be created in the correct order so that the price could be correctly calculated. The individual elements of the meal may also be complex objects in their own right.

public class MealDirector
{
    public void MakeMeal(MealBuilder mealBuilder)
    {
        mealBuilder.AddSandwich();
        mealBuilder.AddSideOrder();
        mealBuilder.AddDrink();
        mealBuilder.AddOfferItem();
        mealBuilder.SetPrice();
    }
}


public abstract class MealBuilder
{
    public abstract void AddSandwich();
    public abstract void AddSideOrder();
    public abstract void AddDrink();
    public abstract void AddOfferItem();
    public abstract void SetPrice();
    public abstract Meal GetMeal();
}


public class JollyVegetarianMealBuilder : MealBuilder
{
    private Meal _meal = new Meal();

    public override void AddSandwich() { _meal.Sandwich = "Vegeburger"; }
    public override void AddSideOrder() { _meal.SideOrder = "Fries"; }
    public override void AddDrink() { _meal.Drink = "Orange juice"; }
    public override void AddOfferItem() { _meal.Offer = "Donut voucher"; }
    public override void SetPrice() { _meal.Price = 4.99; }
    public override Meal GetMeal() { return _meal; }
}


public class MischievousMexicanBuilder : MealBuilder
{
    private Meal _meal = new Meal();

    public override void AddSandwich() { _meal.Sandwich = "Spicy burger"; }
    public override void AddSideOrder() { _meal.SideOrder = "Nachos"; }
    public override void AddDrink() { _meal.Drink = "Tequila"; }
    public override void AddOfferItem() { _meal.Offer = "Hat"; }
    public override void SetPrice() { _meal.Price = 5.49; }
    public override Meal GetMeal() { return _meal; }
}


public class Meal
{
    public string Sandwich { get; set; }
    public string SideOrder { get; set; }
    public string Drink { get; set; }
    public string Offer { get; set; }
    public double Price { get; set; }

    public override string ToString()
    {
        return string.Format("{0}\n{1}\n{2}\n{3}\n{4:f2}",
            Sandwich, SideOrder, Drink, Offer, Price);
    }
}

Testing the Builder

We can test the example builder code using a console application. In the following Main method, the two builders are used to create the two varieties of meal. The contents of the meals' properties are outputted to the console using the overridden ToString method.

static void Main(string[] args)
{
    MealDirector director = new MealDirector();
    MealBuilder jvmb = new JollyVegetarianMealBuilder();
    director.MakeMeal(jvmb);
    Meal vegMeal = jvmb.GetMeal();
    Console.WriteLine(vegMeal);
    Console.WriteLine();

    MealBuilder mmmb = new MischievousMexicanBuilder();
    director.MakeMeal(mmmb);
    Meal mexMeal = mmmb.GetMeal();
    Console.WriteLine(mexMeal);
}

/* OUTPUT

Vegeburger
Fries
Orange juice
Donut voucher
4.99

Spicy burger
Nachos
Tequila
Hat
5.49

*/
13 September 2008