zoukankan      html  css  js  c++  java
  • C#设计模式之四建造者模式(Builder Pattern)【创建型】

    意图:

    将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。

    public class Car{

    private IList<string> parts = new List<string>();

    public void Add(string part){

    parts.Add(part);

     public void Show(){

    foreach(var part in parts){

    Console.WriteLine("组装"+ part); 

    }

    public abstract class Builder{

    public abstract void BuildCarDoor();

    public abstract void BuildCarWheel(); 

    public abstract Car GetCar(); 

    }

    public class BMWBuilder:Builder{

    var car = new Car(); 

    public override void BuilderCarDoor(){

     car.Add("add a Door");

    public override void BuilderCarWheel(){

    car.Add("add a Wheel"); 

    public override Car GetCar(){

    return car; 

    public class BYDBuilder:Builder{

    var car = new Car();

    public override void BuilderCarDoor(){

    car.Add("add a Door"); 

    public override void BuilderCarWheel(){

    car.Add("add a Wheel"); 

    public override Car GetCar(){

    return car; 

    public class Director{

    public void Construct(Builder builder){

    builder.BuilderCarDoor();

    builder.BuilderCarWheel(); 

    public class Client(){

    static void Main(){

    var director = new Director();

    var bmwBuilder = new BMWBuilder();

    var bydBuilder = new BYDBuilder();

     

    director.Construct(bmwBuilder);

    var bmwCar = bmwBuilder.GetCar();

    bmwCar.Show();

     

    director.Construct(bydBuilder);

    var bydCar = bydBuilder.GetCar();

    bydCar.Show(); 

    优点:客户端不必知道产品内部组成的细节。

    具体的建造者类之间是相互独立的,容易扩展。 

    缺点:产生了多余的Build对象和Dirextor类。

  • 相关阅读:
    管理者的四种不同授权风格
    centos7 未启用swap导致内存使用率过高。
    Dynamically create a div element with JavaScript/jQuery
    sql server: Parent/Child hierarchy tree view
    视频编解码系列(一)压缩编码基础常识
    mac 常用终端命令
    Linux下安装Python3.6.8
    SQL特殊字符转义
    Ehcache缓存监控
    指标管理体系设计
  • 原文地址:https://www.cnblogs.com/ljdong7/p/12553120.html
Copyright © 2011-2022 走看看