Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand

Posts Tagged ‘builder’

Builder Pattern – A Robot Example

Posted by JP on November 13, 2011

In this article I am trying to demonstrate the usage of Builder Pattern using a Computer Assembling Robot. Builder Pattern is one among the 23 patterns by Gang Of Four.  

Definition of Builder

The GoF says that the Builder design patterns let you “Separate the construction of a complex object from its representation so that the same construction processes can create different representations.”

Problem to Solve

In a computer manufacturing unit, depending on the configuration – they have to create computers.  The challenge is that the computer parts are chosen in the runtime.  The order can contain n number of computers based on the same configuration.  This could be the right example to implement the Builder pattern where each robot will be setup with a particular computer configuration.  The robot will continue creating the same configuration based computer n times.

Computer Peripherals

Processor:   Intel / AMD

Monitor: Samsung / LG

Speakers: None / Stereo / Surround

Execution

The Windows Form UI allows the user to choose the configuration.  The virtual factory provides up to 3 robots to be added before the actual assembling takes place.

1.gif

Code Explained

Following is the screen shot of the main class named Robot.  The class contains a property called Peripherals which can be used to configure the peripherals added: like CPU as Intel, Monitor as Samsung etc.

After adding the peripherals, we can call the Create() method.  It will iterate through the Peripherals dictionary and calls the appropriate methods to integrate the peripherals.  This allows us to choose the peripherals at run time as well as the sequence too.  

If the Peripheral dictionary contains CPU then the AssembleCPU() method is invoked.

If the Peripheral dictionary contains Monitor then the AssembleMonitor () method is invoked.

2.gif

The class Peripheral is an enumeration as following:

public enum Peripheral

{

Processor,

Monitor,

Speakers

}

The associated source code can be found here.

Advertisements

Posted in C#, Design Patterns | Tagged: , , | Leave a Comment »

Builder Pattern through a Robot Example

Posted by JP on October 8, 2010

Builder Pattern

In this article I am trying to demonstrate the usage of Builder Patter using a Computer Assembling Robot.  Builder Pattern is one among the 23 patters by Gang Of Four. 

Definition of Builder

The GoF says that the Builder design patterns let you “Separate the construction of a complex object from its representation so that the same construction processes can create different representations.”

Problem to Solve

In a computer manufacturing unit, depending on the configuration – they have to create computers.  The challenge is that the computer parts are chosen in the runtime.  The order can contain n number of computers based on the same configuration.  This could be the right example to implement the Builder pattern where each robot will be setup with a particular computer configuration.  The robot will continue creating the same configuration based computer n times.

Computer Peripherals

Processor:   Intel / AMD

Monitor: Samsung / LG

Speakers: Yes / No

Execution

The Windows Form UI allows the user to choose the configuration.  The virtual factory provides upto 3 robots to be added before the actual assembling takes place.

clip_image002[4]

Code Explained

Following is the screen shot of the main class named Robot.  The class contains a property called Peripherals which can be used to configure the peripherals added: like CPU as Intel, Monitor as Samsung etc.

After adding the peripherals, we can call the Create() method.  It will iterate through the Peripherals dictionary and calls the appropriate methods to integrate the peripherals.  This allows us to choose the peripherals at run time as well as the sequence too. 

 

If the Peripheral dictionary contains CPU then the AssembleCPU() method is invoked.

If the Peripheral dictionary contains Monitor then the AssembleMonitor () method is invoked.

 

public partial class Robot : UserControl

    {

        /// <summary>

        /// Dictionary containing peripheral and the option

        /// </summary>

        public Dictionary<Peripheral, string> Peripherals = new Dictionary<Peripheral, string>();

 

        /// <summary>

        /// The core method used to create different representations

        /// </summary>

        public void Create()

        {

            while (true)

            {

                // Create new Computer

                _computer = new Bitmap(Images.Computer);

 

                foreach (Peripheral peripheral in Peripherals.Keys)

                {

                    if (peripheral == Peripheral.Processor)

                        AssembleCPU(Peripherals[peripheral]);

 

                    if (peripheral == Peripheral.Monitor)

                        AssembleMonitor(Peripherals[peripheral]);

 

                    if (peripheral == Peripheral.Speakers)

                        AssembleSpeakers(Peripherals[peripheral]);

 

                }

 

                AddToStore();

            }

        }

 

        public Robot()

        {

            InitializeComponent();

        }

 

        private Bitmap _computer;

 

        private PictureBox _pictureBox;

 

        private void AddToStore()

        {

            _pictureBox = new PictureBox();

            _pictureBox.BorderStyle = BorderStyle.FixedSingle;

            _pictureBox.Image = _computer;

            _pictureBox.Height = _computer.Height;

            _pictureBox.Width = _computer.Width;

 

            try

            {

                MyDelegate del = new MyDelegate(AddToParent);

                IAsyncResult result = Store.BeginInvoke(del);

                while (!result.IsCompleted)

                    Application.DoEvents();

            }

            catch

            {

                // As no stop button provided we have to abort the thread

                Thread.CurrentThread.Abort();

            }

        }

 

        private void AddToParent()

        {

            Store.Controls.Add(_pictureBox);

 

            if (Store.Controls.Count > 1)

            {

                Control lastControl = Store.Controls[Store.Controls.Count – 2];

                _pictureBox.Top = lastControl.Top + lastControl.Height + 1;

                Store.ScrollControlIntoView(_pictureBox);

            }

        }

 

        private delegate void MyDelegate();

 

        private const int Delay = 500;

 

        public void AssembleCPU(string model)

        {

            AddLog("Assembling CPU " + model + "..");

 

            Thread.Sleep(Delay);

            Application.DoEvents();

        }

 

        public void AssembleMonitor(string model)

        {

            AddLog("Assembling Monitor " + model + "..");

 

            Thread.Sleep(Delay);

            Application.DoEvents();

        }

 

        public void AssembleSpeakers(string model)

        {

            AddLog("Assembling Speakers " + model + "..");

 

            if (model == "Stereo")

                _computer = new Bitmap(Images.ComputerWith2Speaker);

            else

                _computer = new Bitmap(Images.ComputerWithSurroundSpeakers);

 

            Thread.Sleep(Delay);

            Application.DoEvents();

        }

 

        private void AddLog(string msg)

        {

            LogBox.Items.Add(msg);

 

            LogBox.SelectedIndex = LogBox.Items.Count – 1;

        }

    }

The class Peripheral is an enumeration as following:

    public enum Peripheral

    {

        Processor,

        Monitor,

        Speakers

    }

 You can find the entire article and source code in c-sharpcorner.com

Posted in C# | Tagged: , , , | Leave a Comment »