Home  /  Questions  /  Question



50   50
Sep 30, 2012


Builder Pattern what about parameters

Hello, in the examples, both here and elswewhere on the internet I see always something like this

public void Construct(Builder builder)

    {

      builder.BuildPartA();

      builder.BuildPartB();

    }
But what if the parts require parameters? How should I provide them? What if for one builder BuildPartA() requires parameters and for an other one it doesn't?
 



90   96.5
Oct 01, 2012
Quickly put in here - you could optimize initializations as required (like Singleton etc.,)

Create and Entity Class that encapsulates all the parameters you may need for PartA,B as Properties

public class PartsDTO
{

string _Property1=string.empty; //for simplicity considered a string property
public string Property1
{
  get{return _Property1;}
set {_Property1=value;}
}

}


public class Builder
{

   public _PartsDTO=null;
   
   public PartsDTO PartsDTO
{
 get{return _PartsDTO;}
set{_PartsDTO=value;}


}

and within the construct method

public void Construct(Builder builder)

    {

      builder.BuildPartA(builder.PartsDTO.Property1);

      builder.BuildPartB();

    }



thanks,
Tarriq