Builder Pattern - The Basic with example

Builder pattern is a creational Pattern. If you use Builder pattern then we can capable to make our application more extensible & loosely couple. In builder paatern 1 extrac class Director and 1 extra interface IBuilder is required. Client create Director object and create IBuilder type object conditionally and pass this object to Director object. Director object call Builder objects method for required operation. I show a code example. This is for very beginers who wants to learn builder desing pattern. Please see the code and write that code on your test project. Then you can easily understand.

namespace TestConsole
{


class Program
{
static void Main(string[] args)
{
string carName = Console.ReadLine();
Director direct = new Director();
ICarBuilder car = null;
if (carName.ToUpper() == "SUZUKI")
{
car = new Suzuki();
}
else if (carName.ToUpper() == "MARCIDISE")
{
car = new Marcidise();
}
direct.ConstructCar(car);
Console.ReadKey();
}
}


interface ICarBuilder
{
void CreateCar();
}
class Suzuki : ICarBuilder
{
public void CreateCar()
{
Console.WriteLine("Suzuki Card is Created. Thanks for Bye Suzuki.");
}
}

class Marcidise :ICarBuilder
{
public void CreateCar()
{
Console.WriteLine("Marcidesce car is created. You will becode proude for using Marcidise");
}
}


class Director
{
public void ConstructCar(ICarBuilder builder)
{
builder.CreateCar();
}
}


}

1 comment:

Anonymous said...

Predilection casinos? enquiry this unfledged [url=http://www.realcazinoz.com]casino[/url] advisor and comprise the office looking for a staunch up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also dilly-dallying our aware [url=http://freecasinogames2010.webs.com]casino[/url] bob at http://freecasinogames2010.webs.com and capture the come about of in true incredibly unseemly !
another late-model [url=http://www.ttittancasino.com]casino spiele[/url] check of events is www.ttittancasino.com , as an variant of german gamblers, awaken sooner than eleemosynary online casino bonus.