C#
  Home arrow C# arrow Page 4 - Creational Patterns in C#
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Creational Patterns in C#
By: Rajesh V S
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 56
    2003-05-15

    Table of Contents:
  • Creational Patterns in C#
  • The Factory Method Pattern
  • The Abstract Factory Pattern
  • The Builder Pattern
  • The Prototype Pattern
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creational Patterns in C# - The Builder Pattern


    (Page 4 of 6 )

    The Builder Pattern comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects. The Builder Pattern separates the construction of a complex object from its representation so that several different representations can be created depending on the needs of the program.

    “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” ” -- "Design Patterns” Gamma et al., Addison-Wesley, ISBN:0-201-63361-2”

    Builder is an object creational design pattern that codifies the construction process outside of the actual steps that carries out the construction - thus allowing the construction process itself to be reused.

    Non-software Example

    Fast food restaurants to construct children’s meals use this pattern. Children's meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy car). Note that there can be variation in the content of the children's meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same.

    The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]

    Another example for Builder pattern is a Computer Assembly. A computer is nothing but the bundling of various components like FDD, HDD, Monitor etc. But when an user buys a computer someone assemble all these components and given to us. Remember that here the building process is completely hidden from the client or user.

    The UML diagram for a Builder pattern is more or less like following one.

    Remember that a project can contain one or more builders and each builder is independent of others. This will improves the modularity and makes the addition of other builders relatively simple. Since each builder constructs the final product step by step, we have more control over the final product that a builder constructs.

    C# Implementation

    //Creational Pattern: BUILDER
    //Author: rajeshvs@msn.com
    using System;
    class Director
    {
     public void Construct(IBuilder builder)
     {
      builder.DoIt();
     }
    }
    interface IBuilder
    {
     void DoIt();
    }
    class BuilderA : IBuilder
    {
     public void DoIt()
     {
      //Necessary code for building the computer type A
      Console.WriteLine("Assembly a Computer with mono monitor");
     }
    }
    class BuilderB : IBuilder
    {
     public void DoIt()
     {
      //Necessary code for building the computer type B
      Console.WriteLine("Assembly a Computer with color monitor");
     }
    }
    class MyClient
    {
     public static void Main()
     {
      Director d = new Director();
      IBuilder build = new BuilderA();
      d.Construct(build);
     }
    }

    More C# Articles
    More By Rajesh V S


     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT