Oct 08 2024
public class Car
{
public string Make { get; }
public string Model { get; }
public int Year { get; }
public string Color { get; }
public Car(string make, string model, int year, string color)
{
Make = make;
Model = model;
Year = year;
Color = color;
}
}
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public string Color { get; set; }
public override string ToString()
{
return $"{Year} {Color} {Make} {Model}";
}
}
public class CarBuilder
{
private readonly Car _car = new Car();
public CarBuilder WithMake(string make)
{
_car.Make = make;
return this;
}
public CarBuilder WithModel(string model)
{
_car.Model = model;
return this;
}
public CarBuilder WithYear(int year)
{
_car.Year = year;
return this;
}
public CarBuilder WithColor(string color)
{
_car.Color = color;
return this;
}
public Car Build()
{
return _car;
}
}
Car car = new CarBuilder()
.WithMake("Tesla")
.WithModel("Model S")
.WithYear(2023)
.WithColor("White")
.Build();
Console.WriteLine(car); // Output: 2023 White Tesla Model S
public class Engine
{
public int HorsePower { get; set; }
public string Type { get; set; }
}
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public Engine Engine { get; set; }
}
public class EngineBuilder
{
private readonly Engine _engine = new Engine();
public EngineBuilder WithHorsePower(int hp)
{
_engine.HorsePower = hp;
return this;
}
public EngineBuilder WithType(string type)
{
_engine.Type = type;
return this;
}
public Engine Build()
{
return _engine;
}
}
public class CarBuilder
{
private readonly Car _car = new Car();
private readonly EngineBuilder _engineBuilder = new EngineBuilder();
public CarBuilder WithMake(string make)
{
_car.Make = make;
return this;
}
public CarBuilder WithModel(string model)
{
_car.Model = model;
return this;
}
public EngineBuilder WithEngine()
{
return _engineBuilder;
}
public Car Build()
{
_car.Engine = _engineBuilder.Build();
return _car;
}
}
Car car = new CarBuilder()
.WithMake("Ford")
.WithModel("Mustang")
.WithEngine()
.WithHorsePower(450)
.WithType("V8")
.Build();
Console.WriteLine(car); // Output: Ford Mustang with 450 HP V8 engine
public class Car
{
public string Make { get; }
public string Model { get; }
public int Year { get; }
public string Color { get; }
private Car(string make, string model, int year, string color)
{
Make = make;
Model = model;
Year = year;
Color = color;
}
public class Builder
{
private string _make;
private string _model;
private int _year;
private string _color;
public Builder WithMake(string make)
{
_make = make;
return this;
}
public Builder WithModel(string model)
{
_model = model;
return this;
}
public Builder WithYear(int year)
{
_year = year;
return this;
}
public Builder WithColor(string color)
{
_color = color;
return this;
}
public Car Build()
{
return new Car(_make, _model, _year, _color);
}
}
}
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 14,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 14,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.