The Builder Design Pattern is a creational design pattern that provides a flexible solution to constructing complex objects. It allows an object to be constructed step-by-step, separating the construction process from the representation. This pattern is particularly useful when an object can be created in multiple ways or when the creation process involves many optional parameters or complex configurations.
-
Separation of Construction and Representation: The Builder Pattern separates the construction of an object from its final representation, allowing the same construction process to create different representations.
-
Step-by-Step Construction: he object is constructed step-by-step through a series of method calls. This makes the creation process more flexible and easier to manage, especially when dealing with complex objects with many fields or configurations.
-
Immutability: In many implementations, once the object is built, it is immutable, meaning its state cannot be changed. This ensures that the object is fully configured when it is constructed and remains consistent throughout its lifetime.
-
Product Class OperatingSystemConfig: The
OperatingSystemConfigclass represents the final product, which is a configuration of an operating system. It has properties like OSName, Version, FileSystem, IsFirewallEnabled, and NetworkSettings. TheDisplayConfig()method is used to print out the configuration details. -
Builder Interface IOperatingSystemConfigBuilder: The
Build()method returns the finalOperatingSystemConfigobject. -
Concrete Builder OperatingSystemConfigBuilder: The
OperatingSystemConfigBuilderclass implements theIOperatingSystemConfigBuilderinterface. It maintains an instance ofOperatingSystemConfigand provides concrete implementations of the methods to set each configuration property. The builder methods return this to allow for method chaining, which is a common practice in builder pattern implementations.
class Program
{
static void Main(string[] args)
{
// Building a Linux OS configuration using the builder
OperatingSystemConfig linuxConfig = new OperatingSystemConfigBuilder()
.SetOSName("Linux")
.SetVersion("5.15")
.SetFileSystem("ext4")
.EnableFirewall(true)
.SetNetworkSettings("DHCP")
.Build();
linuxConfig.DisplayConfig();
// Building a Windows OS configuration using the builder
OperatingSystemConfig windowsConfig = new OperatingSystemConfigBuilder()
.SetOSName("Windows")
.SetVersion("11")
.SetFileSystem("NTFS")
.EnableFirewall(true)
.SetNetworkSettings("Static IP")
.Build();
windowsConfig.DisplayConfig();
}
}