Definition of Builder Pattern: The Builder pattern separates the specification of a complex object from its actual construction. The same construction process can create different representations.
well i have a code sample of Builder Pattern like this
class Director
{
// Build a Product from several parts
public void Construct(IBuilder builder)
{
builder.BuildPartA();
builder.BuildPartB();
builder.BuildPartB();
}
}
interface IBuilder
{
void BuildPartA();
void BuildPartB();
Product GetResult();
}
class Builder1 : IBuilder
{
private Product product = new Product();
public void BuildPartA()
{
product.Add("PartA ");
}
public void BuildPartB()
{
product.Add("PartB ");
}
public Product GetResult()
{
return product;
}
}
class Builder2 : IBuilder
{
private Product product = new Product();
public void BuildPartA()
{
product.Add("PartX ");
}
public void BuildPartB()
{
product.Add("PartY ");
}
public Product GetResult()
{
return product;
}
}
class Product
{
List<string> parts = new List<string>();
public void Add(string part)
{
parts.Add(part);
}
public void Display()
{
Console.WriteLine("\nProduct Parts -------");
foreach (string part in parts)
Console.Write(part);
Console.WriteLine();
}
}
public class Client
{
public static void Main()
{
// Create one director and two builders
Director director = new Director();
IBuilder b1 = new Builder1();
IBuilder b2 = new Builder2();
// Construct two products
director.Construct(b1);
Product p1 = b1.GetResult();
p1.Display();
director.Construct(b2);
Product p2 = b2.GetResult();
p2.Display();
Console.Read();
}
}
Now, i have made some changes in the above code. Is the below code still represent a Builder Pattern?
class Director
{
public void Construct(IBuilder builder)
{
builder.BuildPartA();
builder.BuildPartB();
builder.GetResult();
}
}
interface IBuilder
{
void BuildPartA();
void BuildPartB();
void GetResult();
}
class Builder1 : IBuilder
{
List<string> product = new List<string>();
public void BuildPartA()
{
product.Add("PartA ");
}
public void BuildPartB()
{
product.Add("PartB ");
}
public void GetResult()
{
foreach (var p in product)
{
Console.WriteLine("the product created is :" + p);
}
}
}
class Builder2 : IBuilder
{
List<string> product = new List<string>();
public void BuildPartA()
{
product.Add("PartX ");
}
public void BuildPartB()
{
product.Add("PartY ");
}
public void GetResult()
{
foreach (var p in product)
{
Console.WriteLine("the product created is :" + p);
}
}
}
public class Client
{
public static void Main()
{
Director director = new Director();
IBuilder b1 = new Builder1();
IBuilder b2 = new Builder2();
director.Construct(b1);
director.Construct(b2);
Console.Read();
}
}
note:
I have removed the product class from the second sample code.
My question is, creating a class which handles all the object and call the method in a orderly fashion makes it a Builder Pattern? Just wanted to make sure that I understood the concept of Builder Pattern. Thanks in advance
UPDATE 1
i really couldn't understand why the second sample is not a Builder Pattern. Let me create a second example which would be simple for me to understand and would be simple for others to help me understand. The below is the code.
interface IRoutine {
void WakeUp();
void GoToSchool();
void ListenToMusic();
void Sleep();
}
class person1 : IRoutine
{
public void GoToSchool()
{
Console.WriteLine("going to school");
}
public void ListenToMusic()
{
Console.WriteLine("litening to music");
}
public void Sleep()
{
Console.WriteLine("tring to sleep");
}
public void WakeUp()
{
Console.WriteLine("good morning !!!");
}
}
class person2 : IRoutine
{
public void GoToSchool()
{
Console.WriteLine("going to school");
}
public void ListenToMusic()
{
Console.WriteLine("litening to music");
}
public void Sleep()
{
Console.WriteLine("tring to sleep");
}
public void WakeUp()
{
Console.WriteLine("good morning !!!");
}
}
class Builder {
public void Construct(IRoutine routine) {
routine.WakeUp();
routine.GoToSchool();
routine.ListenToMusic();
routine.Sleep();
}
}
class Client {
static void Main() {
Builder builder = new Builder();
IRoutine r1 = new person1();
IRoutine r2 = new person2();
builder.Construct(r1);
builder.Construct(r2);
Console.Read();
}
}
Is the recent example which i have provided a Builder Pattern? If not then why it is not a Builder Pattern, please help me understand.