Builder Design Pattern

This design pattern is come under Creational Pattern in .Net. It is pattern is one of the simplest design patterns .Using this design pattern we can refer newly created objects through a common interface. The logic of an object is independent from the parts that actually compose the object .It allow different representations for the objects that are being built.

The following are the components of Builder pattern:

Builder class: Represents an abstract interface for creating parts of a Product object.
Concrete Builder: Represents parts of the product by implementing the Builder interface. It defines and keeps track of the representation it creates and provides an interface for saving the product.
Director class: Represents the Product object using the Builder interface.
Product: Represents the object that is being built.

Real Time Usage:

This design pattern can use for Student Administration Form for a real time example. There are different type of users for access the Student administration system. Like Student, Admin, Guest user etc… So we needs to hide some of the buttons based on the user login. This scenario this design pattern we can use.

Example.
In this example I have shown an example of one customer is choosing different type builders & their Flat details.

    /// <summary>
    /// Product Implementation
    /// </summary>
    public class Flat
    {
        public List<string> Type { get; set; }
        public int Floor { get; set; }
        public List<int> Sqft { get; set; }
        public int Rate { get; set; }
        public List<string> Others { get; set; }
 
        public Flat()
        {
            Type = new List<string>();
            Sqft = new List<int>();
            Others = new List<string>();
        }
        /// <summary>
        /// For displaying the Flat details
        /// </summary>
        public void GetFlatDetails()
        {
            Console.WriteLine("Flat Types...:\n");
            foreach (var item in Type)
            {
                Console.Write("\t " + item.ToString());
            }
            Console.WriteLine("\n Floor Plan:" + Floor);
 
            Console.WriteLine("\n Sqft..............");
            foreach (var item in Sqft)
            {
                Console.Write("\t " + item.ToString());
            }
            Console.WriteLine("\n Rate :" + Rate);
            Console.WriteLine("Others");
            foreach (var item in Others)
            {
                Console.Write("\t " + item.ToString());
            }
            Console.WriteLine("");
        }
    } 

 /// <summary>
    /// Builder implementation
    /// </summary>
    public interface IBuilder
    {
        void DesignType();
        void DesignFloor();
        void DesignSqft();
        void SetRate();
        void Others();
 
        Flat GetFlat();
    }
 
   /// <summary>
    /// Concrete Builder1 Implementation
    /// </summary>
    public class PSBBuilders : IBuilder
    {
        Flat PSBFlat = new Flat();
 
        public void DesignType()
        {
            PSBFlat.Type.Add("1 BHK");
            PSBFlat.Type.Add("2 BHK");
            PSBFlat.Type.Add("3 BHK");
        }
        public void DesignFloor()
        {
            PSBFlat.Floor = 5;
        }
        public void DesignSqft()
        {
            PSBFlat.Sqft.Add(650);
            PSBFlat.Sqft.Add(890);
            PSBFlat.Sqft.Add(950);
        }
 
        public void SetRate()
        {
            PSBFlat.Rate = 2850;
        }
        public void Others()
        {
            PSBFlat.Others.Add("WiFi Connected.");
            PSBFlat.Others.Add("24 hrs Genset Back Up.");
        }
 
        public Flat GetFlat()
        {
            return PSBFlat;
        }
    }

    /// <summary>
    /// Concrete Builder2 Implementation
    /// </summary>
    public class ABCBuilders : IBuilder
    {
        Flat AbcFlat = new Flat();
        public void DesignType()
        {
            AbcFlat.Type.Add("1 BHK");
            AbcFlat.Type.Add("2 BHK2T");
            AbcFlat.Type.Add("3 BHK");
            AbcFlat.Type.Add("3 BHK2T");
        }
 
        public void DesignFloor()
        {
            AbcFlat.Floor = 7;
        }
 
        public void DesignSqft()
        {
            AbcFlat.Sqft.Add(650);
            AbcFlat.Sqft.Add(890);
            AbcFlat.Sqft.Add(950);
            AbcFlat.Sqft.Add(1050);
        }
 
        public void SetRate()
        {
            AbcFlat.Rate = 3250;
        }
 
        public void Others()
        {
            AbcFlat.Others.Add("WiFi Connected.");
            AbcFlat.Others.Add("24 hrs Genset Back Up.");
            AbcFlat.Others.Add("24 hrs Security.");
            AbcFlat.Others.Add("ClubHouse.");
        }
 
        public Flat GetFlat()
        {
            return AbcFlat;
        }
    }

    /// <summary>
    /// Director Component implementation
    /// </summary>
    public class ChooseBuilder
    {
        private readonly IBuilder Flats;
 
        public ChooseBuilder(IBuilder builder)
        {
            Flats = builder;
        }
 
        public void CreateFlat()
        {
            Flats.DesignType();
            Flats.DesignFloor();
            Flats.DesignSqft();
            Flats.SetRate();
            Flats.Others();
        }
 
        public Flat GetFlat()
        {
            return Flats.GetFlat();
        }
     } 
 
The Client Call
            ChooseBuilder builder = new ChooseBuilder(new PSBBuilders());
            builder.CreateFlat();
            Flat flat = builder.GetFlat();
            flat.GetFlatDetails();
 
            Console.WriteLine("\n--------------------------\n");
            ChooseBuilder builder1 = new ChooseBuilder(new ABCBuilders());
            builder1.CreateFlat();
            Flat flat1 = builder1.GetFlat();
            flat1.GetFlatDetails();
 
            Console.ReadLine();
 

Out put:
 

No comments:

Post a Comment