builder design pattern

Smart
Posted by Smart under ASP.NET category on | Points: 40 | Views : 376
Introduction

The builder pattern is an object creation software design pattern.Builder pattern builds a complex object using simple objects and using a step bystep approach. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Background

According to the GoF book, the builder design pattern is used to “Separate the construction of a complex object from its representation so that the same construction process can create different representations”. Like most of the GoF book, that is an accurate if dull description.

This description is dealing with the creation of library card of any institute, whose construction is made through simple process.Library card can be of two types ,one is of student and other is of faculty .

We have the following classes.
1.Builder class i.e Library Card Builder which is abstract class having methods to be over ridden in inherited class .



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Builder_design_pattern

{

abstract class LibraryCardBuilder

{ public Lib_card Lib_card { get; set; }

public abstract void Form_Filled();

public abstract void Form_Approved_ByLibrarian();

public abstract void Made_idCard();

}

}


2.Second we have Stdent_Lib_Card is over ridding the methods .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Builder_design_pattern
{
class Stdent_Lib_Card : LibraryCardBuilder
{
public Stdent_Lib_Card()
{
Lib_card = new Lib_card(lib_card_type.student_Lib_card);


}

public override void Form_Filled()
{
Lib_card.name = "Student name";
Lib_card.age = "student age";
Lib_card.department = "Student_department";
Lib_card.Father_name = "student father name";
}
public override void Form_Approved_ByLibrarian()
{
Lib_card.approved = "Library card signed";

}
public override void Made_idCard()
{
Console.WriteLine("student lib card made");
}
}
}

3.Same work as is done in student_Lib_Card is done in Faculty_Lib_card

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Builder_design_pattern
{
class Faculty_Lib_Card:LibraryCardBuilder
{


public Faculty_Lib_Card()
{
Lib_card = new Lib_card(lib_card_type.student_Lib_card);


}

public override void Form_Filled()
{
Lib_card.name = "faculty name";
Lib_card.age = "faculty age";
Lib_card.department = "faculty";
Lib_card.Father_name = "faculty father name";
}
public override void Form_Approved_ByLibrarian()
{
Lib_card.approved = "Library card signed";

}
public override void Made_idCard()
{
Console.WriteLine("faculty library card made");

}
}
}


4.And finally we get our libray card build here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Builder_design_pattern
{
class Lib_card
{
private lib_card_type _lib_card_type;
public Lib_card(lib_card_type lib_card_type)
{
_lib_card_type = lib_card_type;
}
public string age { get; set; }

public string department { get; set; }

public string name { get; set; }

public string Father_name { get; set; }
public string approved { get; set; }



public void Display_details()
{
string Detail;

Detail = string.Format("name: {0}", name);
Console.WriteLine(Detail);

Detail = string.Format("department: {0}", department);
Console.WriteLine(Detail);

Detail = string.Format("age: {0}", age);
Console.WriteLine(Detail);

Detail = string.Format("Father_name: {0}", Father_name);
Console.WriteLine(Detail);




Console.WriteLine();
}



}
}

We also have our library classs that is interacting with Lobrary card Builder.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Builder_design_pattern
{
class Library
{

public void made_id_card(LibraryCardBuilder LibraryCardBuilder)
{
LibraryCardBuilder.Form_Filled();
LibraryCardBuilder.Form_Approved_ByLibrarian();
LibraryCardBuilder.Made_idCard();
}
}
}

calling of all is done over here..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Builder_design_pattern
{
class Program
{
static void Main(string[] args)
{

Library library = new Library();
LibraryCardBuilder LibraryCardBuilder;

LibraryCardBuilder = new Stdent_Lib_Card();
library.made_id_card(LibraryCardBuilder);
LibraryCardBuilder.Lib_card.Display_details();

LibraryCardBuilder = new Faculty_Lib_Card();
library.made_id_card(LibraryCardBuilder);
LibraryCardBuilder.Lib_card.Display_details();


Console.ReadKey();
Console.WriteLine();
}
}
}

Comments or Responses

Login to post response