zoukankan      html  css  js  c++  java
  • Builder Patterns in C#

    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抯 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);
               
    }
    }

  • 相关阅读:
    Delphi下Treeview控件基于节点编号的访问
    oracle的conn / as sysdba是以sys还是system用户登录呢?
    delphi 字母加数字如何自增??比如0A--0Z,1A--1Z一直到9A--9Z 请赐教
    ORACLE_HOME要怎么配置?
    sqlplus / as sysdba 详解
    oracle 11G数据库实例增加内存
    SQL在字符串中取出最长数字子序列
    delphi 全局变量的定义与初始化赋值
    Delphi公用函数单元
    Dapper的正确使用姿势
  • 原文地址:https://www.cnblogs.com/umlchina/p/12470.html
Copyright © 2011-2022 走看看