Builder Design Pattern with easy Code Examples
I have explained the following in this :
// "Director" class Director { IBuilder builder; // A series of steps-in real life, steps are complex. public void Construct(IBuilder builder) { this.builder = builder; builder.StartUpOperations(); builder.BuildBody(); builder.InsertWheels(); builder.AddHeadlights(); builder.EndOperations(); } }
- Creational design patterns
- What is Builder #DesignPattern C# with very easy code example
- Why Builder Design Pattern is important to understand
- Cover the use cases of Builder Design Pattern
- Advantages & Code go through
// "Director" class Director { IBuilder builder; // A series of steps-in real life, steps are complex. public void Construct(IBuilder builder) { this.builder = builder; builder.StartUpOperations(); builder.BuildBody(); builder.InsertWheels(); builder.AddHeadlights(); builder.EndOperations(); } }
// Builders common interface
interface IBuilder
{
void StartUpOperations();
void BuildBody();
void InsertWheels();
void AddHeadlights();
void EndOperations();
Product GetVehicle();
}
// ConcreteBuilder: Car
class Car : IBuilder
{
private string brandName;
private Product product;
public Car(string brand)
{
product = new Product();
this.brandName = brand;
}
public void StartUpOperations()
{
//Starting with brandname
product.Add(string.Format("Car Model name :{0}", this.brandName));
}
public void BuildBody()
{
product.Add("This is a body of a Car");
}
public void InsertWheels()
{
product.Add("4 wheels are added");
}
public void AddHeadlights()
{
product.Add("2 Headlights are added");
}
public void EndOperations()
{
//Nothing in this case
}
public Product GetVehicle()
{
return product;
}
}
// ConcreteBuilder:Motorcycle
class MotorCycle : IBuilder
{
private string brandName;
private Product product;
public MotorCycle(string brand)
{
product = new Product();
this.brandName = brand;
}
public void StartUpOperations()
{
//Starting with brandname
product.Add(string.Format("Motorcycle Model name :{0}", this.brandName));
}
public void BuildBody()
{
product.Add("This is a body of a Motorcycle");
}
public void InsertWheels()
{
product.Add("2 wheels are added");
}
public void AddHeadlights()
{
product.Add("1 Headlights are added");
}
public void EndOperations()
{
//Nothing in this case
}
public Product GetVehicle()
{
return product;
}
}
// "Product"
class Product
{
HttpContext _httpContext => new HttpContextAccessor().HttpContext;
private List<string> parts;
public Product()
{
parts = new List<string>();
}
public void Add(string part)
{
//Adding parts
parts.Add(part);
}
public void Show()
{
_httpContext.Response.WriteAsync("\n\nProduct completed as below :\n");
_httpContext.Response.WriteAsync("********************************\n");
foreach (string part in parts)
_httpContext.Response.WriteAsync(part+"\n");
}
}
MainClass.cs
Director director = new Director();
IBuilder carBuilder = new Car("Ford");
IBuilder motorCycleBuilder = new MotorCycle("Honda");
// Making Car
director.Construct(carBuilder);
Product carProduct = carBuilder.GetVehicle();
carProduct.Show();
//Making MotorCycle
director.Construct(motorCycleBuilder);
Product motorCycleProduct = motorCycleBuilder.GetVehicle();
motorCycleProduct.Show();
Click below to see all these things in action:
Go to Complete Design Pattern List
No comments:
Post a Comment
Your feedback is important.
Visit www.techwebdots.in