Builder mode: a complex object is broken down into several relatively simple parts, which are implemented separately according to different needs, and finally composed of the complex object. The difference between the Builder pattern and the abstract factory approach is that the core of the builder pattern is to break down a complex object into smaller parts and then provide a complex object from the whole to the caller. The latter does not have the concept of disassembly, the various parts of the combination is not a separate object.Copy the code

using System;
namespace CSspace
{
    // You have a home to decorate
    public class House { 
        private string Floor;
        private string Door;
        private string Wall;
        private string Window;
        private string Celling;
    // 1) If you use the constructor directly to generate instance disadvantages:
    // When the instance does not need all the fields, we write various constructors to accommodate various situations.
    // When the argument list of the instance is too long, we should not only pay attention to the order of the arguments but also consider whether there are missing arguments.
    // Write the parameters of the function. (Manual dog head).
    // 
    // 2) if get is used; set; Risks (javaBean patterns) :
    // Get for each field or attribute; The set is done in steps, without continuity.
    // Get between such fields; set; Interspersed with operations that risk changing fields or properties,
    // Don't put off what you can do at once.
    
    // Just privatize the constructor. You're off the job. Give it to Builder, the guy who keeps running around.
        private  House (Builder builder)
            this.Floor = builder.Floor;
            this.Door = builder.Door;
            this.Wall = builder.Wall;
            this.Window = builder.Window;
            this.Celling = builder.Celling;
        }
       public override string ToString()
        {
            return this.Door+"-"+this.Window+"-"+this.Wall+"-"+"-"+this.Floor+"-"+this.Celling;
        }

    // Inner classes are used here
  public class Builder
    {
        public string Door{get;set; }public string Floor{get;set; }public string Wall{get;set; }public string Window{get;set; }public string Celling{get;set; }public Builder(stringdoorstring window){
                this.Door = door;
                this.Window = window;
                }
        // Every time the build section returns to Builder, this place is so cleverly designed. This sets the stage for the next chain call.
        public  Builder  BuildDoor(string door){ this.Door = door; return this; }public  Builder  BuildWall(string wall){this.Wall = wall; return this; }public  Builder  BuildWindow(string window){this.Window = window; return this; }public  Builder  BuildFloor(string floor){this.Floor = floor; return this; }public  Builder  BuildCelling(string celling){this.Celling = celling; return this; }public House build(){
            return new House(this); }}}public class Test {
        public  static void Main(){
           // call ()
           House house = new House.Builder("Security door"."Aluminum window")
           .BuildFloor("Marble floor.")
           .BuildWall("Brush up")
           .BuildCelling("Inherited ceiling") .build(); Console.WriteLine(house.ToString()); }}}Copy the code