Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我们需要构建器设计模式而不是模型?_C#_Oop_Design Patterns_Constructor_Builder - Fatal编程技术网

C# 为什么我们需要构建器设计模式而不是模型?

C# 为什么我们需要构建器设计模式而不是模型?,c#,oop,design-patterns,constructor,builder,C#,Oop,Design Patterns,Constructor,Builder,以下是我对builder设计模式的理解。假设我们有一个如下所示的产品类 public class Product { public string Name { get; set; } public string Description { get; set; } public int NumberOfProducts { get; set; }

以下是我对builder设计模式的理解。假设我们有一个如下所示的产品类

            public class Product
            {
                public string Name { get; set; }

                public string Description { get; set; }

                public int NumberOfProducts { get; set; }

                public decimal Price { get; set; }

                public bool IsDurable { get; set; }
            }
生成器界面:

          public interface IBuilder
          {
            void SetName(string value);

            void SetPrice(decimal value);

            void SetIsDurable(bool value);

            void SetNumberOfProducts(int value);

            void SetDescription(string value);

            Product GetProduct();
        }
具有setter方法并获取产品对象的具体构建器

        public class ConcreteBuilder : IBuilder
        {
            Product product = new Product();

            public Product GetProduct()
            {
                return product;
            }

            public void SetDescription(string value)
            {
                product.Description = value;
            }

            public void SetIsDurable(bool value)
            {
                product.IsDurable = value;
            }

            public void SetName(string value)
            {
                product.Name = value;
            }

            public void SetNumberOfProducts(int value)
            {
                product.NumberOfProducts = value;
            }

            public void SetPrice(decimal value)
            {
                product.Price = value;
            }
        }
在真实场景中,产品的属性应该由用户输入填充,而不是像这样硬编码。在这种情况下,我们还需要发送产品来构建产品对象

        public Product ConstructProduct(IBuilder builder)
        {
            builder.SetDescription("P");
            builder.SetIsDurable(false);
            builder.SetName("My Name");
            builder.SetPrice(10);
            builder.SetNumberOfProducts(5);

            return builder.GetProduct();
        }
在客户端中使用builder对象,如下所示:

public class Client
{
    public void AddProduct()
    {
        ConcreteBuilder builder = new ConcreteBuilder();
        var builtProduct = new Director().ConstructProduct(builder);
        Console.WriteLine(builtProduct.Description);
    }
}
当构造函数中有许多属性要添加时(为了避免伸缩构造反模式),我们为什么不能使用产品模型类本身,而不是使用如上所示的构建器模式呢?如果有任何可选属性,则可以将其设置为可为空

public class Client
{
    Product _prod;

    public Client(Product prod)
    {
        _prod = prod;
    }

    public void AddProduct()
    {
        // Code to add product
        Console.WriteLine(_prod.Description);
    }
}

创建类时,主要目标是降低程序的复杂性。您创建一个类来隐藏复杂性,这样您就不需要考虑它了。当然,你在写这个类的时候需要考虑一下。但是在编写之后,您应该能够忘记细节,在不了解其内部工作的情况下使用该类

显然,在您的问题中提到的情况下,使用builder模式增加了复杂性,仅使用模型是正确的选择。因为创建
产品
对象的过程非常简单

在其他情况下,创建对象可能是一项复杂的操作,您可以使用生成器设计模式来管理这种复杂性


下面是一个实际使用builder模式的示例:

builder模式可以提供很多积极的方面,但也要记住,设计模式只有在有意义时才应该使用。在您的案例中,使用模型可能更有意义(我不确定),但这不仅仅是设计模式的案例-好,没有设计模式-不正确。那么,你能给出一个现实世界中的例子,在那里它可能被使用,而一个模型是不够的