Here, we will explain builder Design Pattern with example in C#.
The Builder Design Pattern is a creational design pattern that helps in constructing complex objects step by step. It separates the construction of an object from its representation, allowing the same construction process to create different representations.
OR
The Builder Design Pattern is a way to build complex objects in a simple and organized manner. Instead of creating the object all at once (which can be confusing when there are many details), the Builder pattern lets you build the object step by step. Each step handles a specific part of the object, making it easier to customize or change the way the object is created.
Example - Imagine building a house. Some houses may have a garage, a garden, and a pool, while others may only have basic features. The Builder pattern lets you choose which features to include, while still following the same steps to build the house.
Let’s take the example of constructing a home. A home might have optional features like a garage, a garden, and a swimming pool, but not all homes have the same set of features. Using the Builder pattern, we can construct homes with different features without having an overly complex constructor.
This way, you can create a luxury house (with all the extras) or a simple house (with just the basics) using the same steps, just by choosing which features to include.
Luxury House
Simple House
Here’s an example demonstrating how to use the Builder pattern to construct different types of homes.
using System;
public class Home
{
public string Foundation { get; set; }
public string Structure { get; set; }
public string Roof { get; set; }
public bool HasGarage { get; set; }
public bool HasGarden { get; set; }
public bool HasSwimmingPool { get; set; }
public override string ToString()
{
return $"Home with {Foundation}, {Structure}, {Roof}, " +
$"Garage: {HasGarage}, Garden: {HasGarden}, Swimming Pool: {HasSwimmingPool}";
}
}
public interface IHomeBuilder
{
void BuildFoundation();
void BuildStructure();
void BuildRoof();
void AddGarage();
void AddGarden();
void AddSwimmingPool();
Home GetHome();
}
public class LuxuryHomeBuilder : IHomeBuilder
{
private Home _home = new Home();
public void BuildFoundation() => _home.Foundation = "Concrete, steel, and glass foundation";
public void BuildStructure() => _home.Structure = "Luxury Structure with 5 bedrooms";
public void BuildRoof() => _home.Roof = "Luxury Roof with solar panels";
public void AddGarage() => _home.HasGarage = true;
public void AddGarden() => _home.HasGarden = true;
public void AddSwimmingPool() => _home.HasSwimmingPool = true;
public Home GetHome() => _home;
}
public class SimpleHomeBuilder : IHomeBuilder
{
private Home _home = new Home();
public void BuildFoundation() => _home.Foundation = "Basic concrete foundation";
public void BuildStructure() => _home.Structure = "Simple Structure with 2 bedrooms";
public void BuildRoof() => _home.Roof = "Simple roof with tiles";
public void AddGarage() => _home.HasGarage = false;
public void AddGarden() => _home.HasGarden = false;
public void AddSwimmingPool() => _home.HasSwimmingPool = false;
public Home GetHome() => _home;
}
public class HomeDirector
{
private IHomeBuilder _homeBuilder;
public HomeDirector(IHomeBuilder homeBuilder)
{
_homeBuilder = homeBuilder;
}
public void ConstructHome()
{
_homeBuilder.BuildFoundation();
_homeBuilder.BuildStructure();
_homeBuilder.BuildRoof();
}
public void AddLuxuryFeatures()
{
_homeBuilder.AddGarage();
_homeBuilder.AddGarden();
_homeBuilder.AddSwimmingPool();
}
public Home GetHome()
{
return _homeBuilder.GetHome();
}
}
class Program
{
static void Main(string[] args)
{
// Build a luxury home
IHomeBuilder luxuryBuilder = new LuxuryHomeBuilder();
HomeDirector director = new HomeDirector(luxuryBuilder);
director.ConstructHome();
director.AddLuxuryFeatures();
Home luxuryHome = director.GetHome();
Console.WriteLine(luxuryHome.ToString());
// Build a simple home
IHomeBuilder simpleBuilder = new SimpleHomeBuilder();
director = new HomeDirector(simpleBuilder);
director.ConstructHome();
Home simpleHome = director.GetHome();
Console.WriteLine(simpleHome.ToString());
}
}
Output
A real-world example could be the process of building a pizza. A pizza has various components: dough, sauce, toppings, cheese, etc. With a builder pattern, you can create different variations of pizza (e.g., vegetarian, meat-lover, thin-crust) by assembling the components in a step-by-step manner.
For instance:
This mirrors the builder pattern, where different kinds of pizzas can be created by specifying their ingredients step by step.