设计模式:生成器 [英] Design Pattern: Builder

查看:80
本文介绍了设计模式:生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在生成器模式(C#)中查找了好的示例,但是找不到一个,因为我不了解Builder模式,或者我试图做一些从未想过的事情。例如,如果我有一个抽象的汽车和抽象的制造商的方法来制造汽车零件,我应该能够把我所有的30个选择发送给总监,让它建立我需要的部件,然后建立我的汽车。无论哪一辆汽车,卡车,半等都是生产的,我应该能够以完全相同的方式开车。

I have looked for a good example of a Builder pattern (in C#), but cannot find one either because I don't understand the Builder pattern or I am trying to do something that was never intended. For example, if I have an abstract automobile and abstract builder methods to create car parts, I should be able to send all 30 of my choices to the Director, have it build the pieces I need, then build my automobile. Regardless of which car, truck, semi, etc. produced, I should be able to "drive" it in exactly the same way.

第一个问题是大多数硬件代码财产值在具体部分,我真的认为应该来自一个数据库。我认为这个想法是将我的选择发送给总监(从数据源),并让建设者根据我的数据创建一个定制的产品。

First problem is most examples hard code property values in to the concrete parts, which I really think should come from a database. I thought the idea was to send my choices to the Director (from a data source) and have the builder create a customized product based on my data.

第二个问题是我希望生成器方法实际创建零件,然后将其分配给产品,而不是传递字符串,而是真正的强类型产品零件。

Second problem is I want the builder methods to actually create the parts then assign them to the product, not pass strings but real strongly typed product parts.

例如,我想创建一个表单通过为我创建一个Builder制作表单字段,包括一个标签,一个输入部分,验证等,这样我可以从我的ORM中读取对象,查看对象的元数据,将其传递给我的Builder,并添加新创建的用户控制结果到我的网络表单。

For example, I want to create a form on the fly by having a Builder manufacture form fields for me, including a label, an input section, validation, etc. This way I can read the object from my ORM, check out the object's metadata, pass this to my Builder and add the newly created user control result to my web form.

然而,每个Builder示例,我发现只有硬编码数据,而不是从主要代码传递给构建器和踢的选择定制产品。一切似乎都是一个很大的静态案例。例如,如果我有三个参数,每个参数有10个选项,我不想构建30个具体的Builder方法,我只想创建足够的制造我的产品所需的属性,可能只有三个。

However, every Builder example I find only has hard coded data instead of passing choices from the main code to the Builder and kicking out a customized product. Everything seems to be a big static case statement. For example, if I have three parameters with 10 choices each, I don't want to build 30 concrete Builder methods, I want to create only enough to manufacture the properties my product requires, which may be only three.

我很想让Director在主代码中存在。应该有一种方式来自动确定哪个具体的构建器方法调用类似于多态和方法重载(尽管这是一个非常糟糕的示例),而不是在模式中使用case语句。 (每次我需要添加一个新的产品类型,我将需要修改现有的Director,这是坏的)。

I am tempted to have the Director exist in the main code only. There should be a way to automatically determine which concrete builder method to call similar to polymorphism and method overloads (although that is a very bad example) instead of using a case statement within the pattern. (Every time I need to add a new product type, I will need to modify the existing Director, which is bad).

推荐答案

我将参考维基百科文章中的C#示例文章 here

I am going refer to the C# example in the Wikipedia Article here.


第一个问题是大多数实例硬
代码属性值到
具体部分,我真的认为
应该来自一个数据库。我认为
的想法是将我的选择发送到
董事(来自数据源),并且
构建器根据我的数据创建一个定制的
产品。

First problem is most examples hard code property values in to the concrete parts, which I really think should come from a database. I thought the idea was to send my choices to the Director (from a data source) and have the builder create a customized product based on my data.

在这种情况下,您将有一个类实现PizzaBuilder知道如何从数据库检索数据。你可以做几种方式。

In this case you would have class implementing PizzaBuilder that knows how to retrieve data from a database. You can do it several ways.

一个人将会是一个夏威夷派披豆。课程初始化时,会查询夏威夷比萨的数据库并检索该行。然后当调用各种Build(x)方法时,它将属性设置为检索到的数据库行的相应字段。

One would be make a HawaiianPizzaBuilder. When the class initializes it queries the database for a Hawaiian Pizza and retrieves the row. Then when the various Build(x) methods are called it would set the properties to the corresponding field of the retrieved database row.

另一个将是一个PizzaDatabaseBuilder,并使确定当您初始化类时,您将传递该类型的匹萨所需的行的ID。例如,而不是

Another would be just makes a PizzaDatabaseBuilder and make sure that when you initialize the class you pass it the ID of the row you need for that type of pizza. For example instead of

waiter.PizzaBuilder = new HawaiianPizzaBuilder();

您使用

waiter.PizzaBuilder = new PizzaDatabaseBuilder("Hawaiian");




Second problem is I want the builder methods to actually create the

零件的构建器方法将它们分配给产品,
不通过字符串但实际上是
类型的产品部件。

parts then assign them to the product, not pass strings but real strongly typed product parts.

不应该是一个问题。您需要的是其他Factory / Builder类型模式来初始化Pizza的字段。例如

Should not be an issue. What you need is an other Factory/Builder type pattern to initialize the fields of the Pizza. For example

而不是

 public override void BuildDough()   { pizza.Dough   = "pan baked"; }

你会做一些像

 public override void BuildDough()   { pizza.Dough   = new DoughBuilder("pan baked"); }

 public override void BuildDough()   { pizza.Dough   = new PanBakedDoughBuilder(); }

DoughBuilder可以转到数据库中的另一个表,以正确填写PizzaDough类。

DoughBuilder can go to another table in your database to properly fill out a PizzaDough Class.

这篇关于设计模式:生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆