【23种GOF设计模式】C#代码完整案例详解--建造者模式

来自:CCNetCore社区,一个关注.Netcore领域的社区团队。

建造者模式BuilderPattern

建造者模式BuilderPattern 创建型设计模式

用于更复杂的对象创建,通过类似与组装的方式进行构建。用户无需知道具体实现细节,只用按照每个提供好的方法进行组装调用即可。

例如:一辆汽车由方向盘、轮胎、灯光进行构建,可将这些制作过程拆开,通过用户调用进行组装。当然,当零件过多的时候,更可以外面包一层,将多个组装方法放入即可。

Program.cs

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

namespace BuilderPattern
{
   /// 
   /// 1 Builder设计模式4大角色
   /// 2 和工厂模式的区别
   /// 
   class Program
   {
       static void Main(string[] args)
       {
           try
           {
               Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的建造者模式Builder");
               Console.WriteLine("**********************************");
               {
                   AbstractBuilder builder = new BuilderFord();
                   builder.Engine();
                   builder.Wheels();
                   builder.Light();

                   builder.Car();
               }
               Console.WriteLine("**********************************");
               {
                   AbstractBuilder builder = new BuilderBYD();
                   builder.Engine();
                   builder.Wheels();
                   builder.Light();

                   builder.Car();
               }
               Console.WriteLine("**********************************");
               {
                   AbstractBuilder builder = new BuilderBYD();

                   Director director = new Director(builder);
                   director.GetCar();
               }

               Console.WriteLine("**********************************");
               {
                   AbstractBuilder builder = new BuilderVolkswagen();
                   Director director = new Director(builder);
                   director.GetCar();
               }

           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
           }
           Console.Read();
       }
   }
}

BuilderFord.cs

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

namespace BuilderPattern
{
   /// 
   /// 福特工人
   /// 
   public class BuilderFord : AbstractBuilder
   {
       private Engine _Engine = null;
       private Wheels _Wheels = null;
       private Light _Light = null;

       public override void Engine()
       {
           this._Engine = new Engine()
           {
               Name = "_Engine"
           };

           Console.WriteLine("{0} build Engine", this.GetType().Name);
       }

       public override void Wheels()
       {
           this._Wheels = new Wheels()
           {
               Name = "_Wheels"
           };
           Console.WriteLine("{0} build Wheels", this.GetType().Name);
       }

       public override void Light()
       {
           this._Light = new Light()
           {
               Name = "_Light"
           };
           Console.WriteLine("{0} build Light", this.GetType().Name);
       }

       public override Car Car()
       {
           Console.WriteLine("组装 {0} {1} {2}", this._Engine, this._Light, this._Wheels);
           Console.WriteLine("{0} build Mondeo", this.GetType().Name);

           return new Car(this._Engine, this._Light, this._Wheels)
           {
               Name = "Mondeo"
           };
       }
   }
}

Director.cs

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

namespace BuilderPattern
{
   public class Director
   {
       private AbstractBuilder _AbstractBuilder = null;
       public Director(AbstractBuilder builder)
       {
           this._AbstractBuilder = builder;
       }

       public Car GetCar()
       {
           this._AbstractBuilder.Engine();
           this._AbstractBuilder.Wheels();
           this._AbstractBuilder.Light();

           return this._AbstractBuilder.Car();
       }
   }
}

AbstractBuilder.cs

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

namespace BuilderPattern
{
   /// 
   /// 工人抽象
   /// 
   public abstract class AbstractBuilder
   {

       public abstract void Engine();

       public abstract void Wheels();

       public abstract void Light();

       public abstract Car Car();
   }
}

Car.cs

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

namespace BuilderPattern
{
   public class Car
   {
       public Car(Engine engine, Light light, Wheels wheels)
       { }

       public string Name { get; set; }
   }
}

Engine.cs

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

namespace BuilderPattern
{
   public class Engine
   {
       public string Name { get; set; }
   }
}

#Light.cs

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

namespace BuilderPattern
{
   public class Light
   {
       public string Name { get; set; }
   }
}

Wheels.cs

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

namespace BuilderPattern
{
   public class Wheels
   {
       public string Name { get; set; }
   }
}

最后,想了解更多,可加入CCNetCore社区,橙子带你走上.netcore学习之路。
你可以免费获取到:

  • 设计模式
  • 面试八股文
  • 问题答疑
  • 闲聊茶馆
  • Asp.Netcore图文源码解读
  • 第一代软件架构单体应用相关技术
  • 第二代软件架构分布式应用相关技术
  • 第三代软件架构微服务应用相关技术
  • 第四代软件架构网格服务应用相关技术

站点网址:ccnetcore.com

你可能感兴趣的:(设计模式,c#,建造者模式,linq,设计模式)