public class Car { private string _make; private string _model; private string _color; public Car(string make, string model, string color) { _make = make; _model = model; _color = color; } public override string ToString() { return $"Make: {_make}, Model: {_model}, Color: {_color}"; } } public interface ICarBuilder { void SetMake(string make); void SetModel(string model); void SetColor(string color); Car GetCar(); } public class CarBuilder : ICarBuilder { private Car _car = new Car("", "", ""); public void SetMake(string make) { _car._make = make; } public void SetModel(string model) { _car._model = model; } public void SetColor(string color) { _car._color = color; } public Car GetCar() { return _car; } } public class Director { private ICarBuilder _builder; public Director(ICarBuilder builder) { _builder = builder; } public void BuildCar() { _builder.SetMake("Toyota"); _builder.SetModel("Camry"); _builder.SetColor("Red"); } } // Usage var builder = new CarBuilder(); var director = new Director(builder); director.BuildCar(); var car = builder.GetCar(); Console.WriteLine(car.ToString());
public class HtmlElement { private string _name; private string _text; private ListBoth of these examples use the BuilderFactory pattern to provide a more modular and scalable approach to object creation. The package/library used is likely the System.Runtime.CompilerServices.Unsafe package, which is sometimes used in conjunction with pattern-based designs like the BuilderFactory._elements = new List (); private const int IndentSize = 2; public HtmlElement(string name, string text = "") { _name = name; _text = text; } public void Add(HtmlElement element) { _elements.Add(element); } public override string ToString() { return ToStringImpl(0); } private string ToStringImpl(int indent) { var sb = new StringBuilder(); var indentString = new string(' ', IndentSize * indent); sb.AppendLine($"{indentString}<{_name}>"); if (!string.IsNullOrWhiteSpace(_text)) { sb.Append(new string(' ', IndentSize * (indent + 1))); sb.AppendLine(_text); } foreach (var element in _elements) { sb.Append(element.ToStringImpl(indent + 1)); } sb.AppendLine($"{indentString}"); return sb.ToString(); } } public interface IHtmlBuilder { void AddElement(string name, string text); void AddChild(string name, string text); HtmlElement GetElement(); } public class HtmlBuilder : IHtmlBuilder { private HtmlElement _root = new HtmlElement("html"); public void AddElement(string name, string text) { var element = new HtmlElement(name, text); _root.Add(element); } public void AddChild(string name, string text) { var parent = _root._elements.LastOrDefault(); if (parent == null) { throw new Exception("Cannot add child to a non-existent parent."); } var element = new HtmlElement(name, text); parent.Add(element); } public HtmlElement GetElement() { return _root; } } public class HtmlDirector { private IHtmlBuilder _builder; public HtmlDirector(IHtmlBuilder builder) { _builder = builder; } public void BuildHtml() { _builder.AddElement("head", ""); _builder.AddChild("title", "Document Title"); _builder.AddElement("body", ""); _builder.AddElement("h1", "Header"); _builder.AddElement("p", "Paragraph"); } } // Usage var builder = new HtmlBuilder(); var director = new HtmlDirector(builder); director.BuildHtml(); var html = builder.GetElement(); Console.WriteLine(html.ToString());