DEV Community

Captain Iminza
Captain Iminza

Posted on • Edited on

Builder Pattern in C#

Introduction

The Builder Pattern is a creational design pattern that allows you to construct complex objects by specifying their type and content. The Builder Pattern separates the construction of an object from its representation, enabling the same construction process to create different representations.

When to Use the Builder Pattern

  • When an object needs to be constructed with many parameters or complex initialization processes.
  • When you want to build an immutable object that should be initialized with all required properties.
  • When you need different representations of an object, but the construction process remains the same.

Structure of the Builder Pattern
The Builder Pattern consists of four main components:

  • Product: The complex object that is being constructed.
  • Builder Interface: Defines all possible ways to build the product.
  • Concrete Builder: Implements the builder interface and constructs the product.
  • Director: Constructs the object using the Builder interface.

Implementation in C#

Let's dive into an example to understand how to implement the Builder Pattern in C#.

Step 1: Define the Product

The product is the complex object that we want to build.
The product is the Mentorship Program which links students to mentors.

Mentorship Program Class

Step 2: Create the Builder Interface

The builder interface specifies the steps required to build the Mentorship Program.

Mentorship Program Interface

Step 3: Implement the Concrete Builder

The concrete builder implements the builder interface and provides specific implementations for each building step.



namespace BuilderPattern.Services.Implementations
{
    public class MentorshipProgramBuilder : IMentorshipProgramBuilder
    {
        private MentorshipProgram _program;

        public MentorshipProgramBuilder()
        {
            _program = new MentorshipProgram();
        }

        public void SetProgramName(string name)
        {
            _program.ProgramName = name;
        }

        public void SetDates(DateTime start, DateTime end)
        {
            _program.StartDate = start;
            _program.EndDate = end;
        }

        public void AddStudent(string student)
        {
            _program.Students.Add(student);
        }

        public void AddMentor(string mentor)
        {
            _program.Mentors.Add(mentor);
        }

        public void PairStudentWithMentor(string student, string mentor)
        {
            _program.StudentMentorPairs[student] = mentor;
        }

        public MentorshipProgram GetProgram()
        {
            return _program;
        }
    }

}


Enter fullscreen mode Exit fullscreen mode

Step 4: Create the Director

The director class is responsible for constructing the object using the builder interface.

Mentorship Program Director Class

Step 5: Putting It All Together

Here's how you can use the Builder Pattern to construct a Mentorship Program object.

MentorshipProgram

Benefits of the Builder Pattern

  • Modularity: Each component of the MentorshipProgram is built in a modular way, allowing for clear separation of concerns.
  • Flexibility: New students or mentors can be added, and existing pairs can be modified without changing the overall construction logic.
  • Scalability: The pattern supports scalability by allowing complex setups to be managed easily, accommodating future expansion of the mentorship program.
  • Clearer Code: The code is more readable and easier to understand, especially when dealing with complex objects.
  • Reusability: The same construction process can create different representations of the product.

By using the Builder Pattern, we can efficiently manage the construction of complex objects like a mentorship program, ensuring our code remains clean, maintainable, and scalable.

Conclusion

The Builder Pattern is a powerful tool in C# for constructing complex objects. By separating the construction process from the representation, it provides flexibility, clarity, and reusability in your code. Whether you are dealing with an object that requires many initialization parameters or different representations, the Builder Pattern is worth considering.

Happy coding!

Top comments (1)

Collapse
 
ganatrajay2000 profile image
Jay Ganatra

You can implement method chaining in your builder for a more fluent interface. Here instead of a big constructor, you have a big builder.ConstructParam().