Builder design Pattern

Builder design Pattern: This pattern is used when objects construction should be separate from its representation, so that same construction process can create different representation.

Example: Here I have taken the example of a House building and Mall Building. Their construction process is same, for x: build floors , build walls, build roofs . But, Of Couse, their representation is different.

Following is the code: Code is also present on GitHub. Click here to go to GitHub

namespace BuillderDesignPattern

{

class Program

{

static void Main(string[] args)

{

var architect = new BuildingArchitect();

 

IBuilder houseBuilder = new HouseBuilder();

IBuilder mallBuilder=new MallBuilder();

 

// Build House

architect.Construct(houseBuilder);

var houseObject = houseBuilder.GetBuildingObject();

Console.WriteLine($”Building Name is :{houseObject.Name}”);

Console.WriteLine($”Floor is :{houseObject.FloorSize}”);

Console.WriteLine($”Numner of walls are :{houseObject.NumberOfWalls}”);

Console.WriteLine($”Roof is :{houseObject.RoofSzie}”);

 

//Build Mall

architect.Construct(mallBuilder);

var mallObject = mallBuilder.GetBuildingObject();

Console.WriteLine($”Building Name is :{mallObject.Name}”);

Console.WriteLine($”Floor is :{mallObject.FloorSize}”);

Console.WriteLine($”Numner of walls are :{mallObject.NumberOfWalls}”);

Console.WriteLine($”Roof is :{mallObject.RoofSzie}”);

 

Console.ReadLine();

}

}

/// <summary>

///

/// </summary>

public class BuildingArchitect

{

public void Construct(IBuilder builder)

{

builder.SetName();

builder.BuildFloor();

builder.BuildWalls();

builder.BuildRoof();

}

}

 

/// <summary>

///

/// </summary>

public interface IBuilder

{

void SetName();

void BuildFloor();

void BuildWalls();

void BuildRoof();

BuildingObject GetBuildingObject();

}

/// <summary>

///

/// </summary>

public class BuildingObject

{

public string Name { get; set; }

public decimal FloorSize { get; set; }

public int NumberOfWalls { get; set; }

 

public decimal RoofSzie { get; set; }

}

 

publicĀ  class HouseBuilder:IBuilder

{

private BuildingObject _buildingObject = new BuildingObject();

 

public void SetName()

{

_buildingObject.Name = “House”;

}

public void BuildFloor()

{

_buildingObject.FloorSize = 100;

}

 

public void BuildWalls()

{

_buildingObject.NumberOfWalls = 10;

}

public void BuildRoof()

{

_buildingObject.RoofSzie = 150;

}

public BuildingObject GetBuildingObject()

{

return this._buildingObject;

}

}

public class MallBuilder : IBuilder

{

private BuildingObject _buildingObject = new BuildingObject();

public void SetName()

{

_buildingObject.Name = “Mall”;

}

public void BuildFloor()

{

_buildingObject.FloorSize = 500;

}

public void BuildWalls()

{

_buildingObject.NumberOfWalls = 45;

}

public void BuildRoof()

{

_buildingObject.RoofSzie = 596;

}

public BuildingObject GetBuildingObject()

{

return this._buildingObject;

}

}

}

Author: Rupesh

Hi! I'm Rupesh, a funophile and technophile. I'm an Application Developer, Solution Architect and IT-Consultant, and an author in the works. I am a Microsoft certified Professional and Solution Developer (MCP and MCSD).

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s