Builder Design Pattern is used to divide the object building operation into pieces. Complete object is created in sequential (step by step) process. At every step operations are performed on the object to enhance its properties. Different rules or restrictions could be set at a step. In the end complete structure will be created. Make the process of constructing an object generic so that it can be used to create different representations of the same object. Using generics this pattern can be highly refined. Few of the advantages of this pattern are as below:

  1. Instead of putting all properties in a constructor, parameters can be passed at different stages/steps
  2. It draws a separation line between representation and construction of the object
  3. Same model can be used to parse more than one complex objects
  4. It is much user friendly. It reduces the effort of the user to understand and use the controls built on this structure
  5. Using generics integration between builder and its schema become tight. As we can easily associate the Concrete builder with Concrete Product as per their types

This is how the structure looks

BuilderDesign

  1. Factory is responsible for creating initial builder object which will generate the final output
  2. Builder provides the base to initialize or build the object
  3. Product is the schema which will be used by Builder to generate the output
  4. Concrete Builder  is the derived class from Builder which exposes the methods (stages/ steps) to enhance property set
  5. Concrete product is the derived class from Product where all corresponding properties are defined

Look at the below code

Type Code
Factory public virtual AccordionBuilder Accordion(){

return new AccordionBuilder();

}

Builder public abstract class ControlBuilder<TControl, TBuilder> : IHtmlString where TControl : Control, new()where TBuilder : ControlBuilder<TControl, TBuilder>{

protected internal TControl Control{get;set;}

public ControlBuilder()

{

this.control = new TControl();

this.control.Option = new ControlOptions();

}

public virtual string ToHtmlString()

{

string result;

using (StringWriter stringWriter = new StringWriter())

{

Control.Render(new HtmlTextWriter(stringWriter));

result = stringWriter.ToString();

}

return result;

}

}

Product public abstract class Control{

public string Id{get; set;}

public ControlOptions Option{get; set; }

public virtual void Render(HtmlTextWriter writer)

{

}

}

Concrete Builder public class AccordionBuilder : ControlBuilder<Accordion, AccordionBuilder>{

public AccordionBuilder(): base()

{

}

public AccordionBuilder Options(Action<AccordionOptionBuilder>addAction)

{

AccordionOptionBuilder obj = new AccordionOptionBuilder();

this.Control.Option = obj.OptionControl;

addAction(obj);

return this;

}

}

Concrete Product public class Accordion : Control{}

 

And then through front end this is what you will be able to achieve

@( Html.Juime().Accordion()

.Options(option =>

{

option.Active = true;

}

)