Builder Design Pattern



In Elizabeth's day care center, the teacher helps the kids to build all kinds of toys to develop their creative skills. One of Elizabeth's favorite activities is to make animals with play-dough.
A set of molds is the tool that Elizabeth always uses to create her favorite cool animals.
One mold tool set includes five parts, including the head, body, arm, leg, and tail. Whenever Elizabeth wants to build an animal, she will use one set of each tools to make a head, body , leg, arm, and tail for that animal, and then assembles them with glue to build an animal. There are many types of animal mold tool sets that the kids can choose from.


For example: if Elizabeth wants to make a monkey, then she will pick the set of monkey molds to start.
  • Step 1. Make monkey head.
  • Step 2. Make monkey body.
  • Step 3. Make monkey leg.
  • Step 4. Make monkey arm.
  • Step 5. Make monkey tail.
Once all the five parts are finished, then Elizabeth will glue them all together and decorate it to have a monkey done as a finished product. Most likely, she will give it to me or her mom as a gift when we pick her (she will not give us the monkey if the monkey is not decorated, since it will not be looking good at all then). When she wants to make a kitten, she follows the same steps with the set of Kitten molds.
In the above scenario, an Builder Design Pattern is perfectly used by Elizabeth in her daily fun activities.





Introduction

The Builder Design Pattern helps us to slice the operations of building an object. It also enforces a process to create an object as a finished product. That means an object has to be massaged by some instructed steps before it is ready and can be used by others. The massage process could apply any restrictions or business rules for a complete building procedure (or a procedure we follow to make an object that is considered as a ready to use object). For instance, to compose an email, you can't leave the To and Subject fields blank before you can send it. In other words, an email object will be considered as an uncompleted email object (a common business rule for email) if those two fields are not filled. It has to be built (filled) before we can send it out. This article introduces an implementation of how we use the Builder Design Pattern for Elizabeth's fun activity.
 








                                                                                                                                                                    







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

//USING BUILDER PATTERN

namespace ManualResetEvent_
{
    interface IFood
    {
        string name();
        int quantity();
        int price();
    }


    public class Roll : IFood
    {
        public string name()
        {
            return "Roll";

        }

        public int quantity()
        {
            return 1;
        }

        public int price()
        {
            return 100;
        }
    }


    public class Chat : IFood
    {
        public string name()
        {
            return "Chat";

        }

        public int quantity()
        {
            return 1;
        }

        public int price()
        {
            return 50;
        }
    }


    public class IceCream : IFood
    {
        public string name()
        {
            return "Ice Cream";

        }

        public int quantity()
        {
            return 1;
        }

        public int price()
        {
            return 120;
        }
    }



    public class Dosa : IFood
    {
        public string name()
        {
            return "Dosa";

        }

        public int quantity()
        {
            return 1;
        }

        public int price()
        {
            return 50;
        }
    }



    public class Idly : IFood
    {
        public string name()
        {
            return "Idly";

        }

        public int quantity()
        {
            return 1;
        }

        public int price()
        {
            return 50;
        }
    }

    class Meal
    {
        public List<IFood> foodIteam = new List<IFood>();
        public void addTeams(IFood food_items)
        {
            foodIteam.Add(food_items);
        }


        public int totalBill()
        {
            int cost=0;
            foreach (IFood items in foodIteam)
            {
                cost = cost + items.price();
            }

            return cost;
        }

        public void show()
        {
            foreach (IFood items in foodIteam)
            {
                Console.Write(items.name() + ",");
            }

        }

    }

    class MealController
    {
        public Meal prepareIceCream()
        {
        Meal icecream = new Meal();
        icecream.addTeams(new IceCream());
        return icecream;
        }

        public Meal preparechat()
        {
            Meal chat = new Meal();
            chat.addTeams(new Chat());
            return chat;
        }


        public Meal preparechat_Icecream()
        {
            Meal chat_icecream = new Meal();
            chat_icecream.addTeams(new Chat());
            chat_icecream.addTeams(new IceCream());
            return chat_icecream;
        }

        public Meal prepareDosa()
        {
            Meal Dosa = new Meal();
            Dosa.addTeams(new Dosa());
            return Dosa;
        }

        public Meal prepareIdly()
        {
            Meal Idly = new Meal();
            Idly.addTeams(new Idly());
            return Idly;
        }




    }

     class People
    {
        public string name {get;set;}
        public int mobile{get;set;}
        public List<string> food;

        public People(string name_, int mobile_)
        {
            name = name_;
            mobile = mobile_;
        }

     }

    class Restaurant
    {
       
        public static void Main()
        {
            People Amit = new People("Amit", 9876);
            MealController icecream = new MealController();
            Meal Icecream_=icecream.prepareIceCream();
            Console.Write("Amit ordered:");
            Icecream_.show();
            Console.WriteLine("Amit Bill:" + Icecream_.totalBill());

            Console.WriteLine("--------------------------------------");

            People Ajit = new People("Ajit", 9876);
            MealController icecream_chat = new MealController();
            Meal Icecream_and_chat_ = icecream_chat.preparechat_Icecream();
            Console.Write("Ajit ordered:");
            Icecream_and_chat_.show();
            Console.WriteLine("Ajit Bill:" + Icecream_and_chat_.totalBill());

            Console.WriteLine("--------------------------------------");

            People Ajay = new People("Ajay", 9876);
            MealController Dosa = new MealController();
            Meal Dosa_= Dosa.prepareDosa();
            Meal Idly = Dosa.prepareIdly();
            Console.Write("Ajay ordered:");
            Dosa_.show();
            Console.WriteLine("Ajay Bill:" + Dosa_.totalBill());
            Idly.show();
            Console.WriteLine("Ajay Bill:" + Idly.totalBill());

            Console.ReadLine();

        }

    }




}

 

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6