设计模式:建造者 [英] Design Pattern: Builder

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

问题描述

我已经看过了 Builder模式的例子(在C#),但不能找到一个或者是因为我不懂Builder模式还是我试图做一些从来没有打算。举例来说,如果我有一个抽象的汽车和抽象建设者方法来创建汽车零部件,我应该能送我的选择署长所有30个,已建它,我需要的作品,然后建立我的汽车。无论哪个汽车,卡车,半等制作的,我应该能够以完全相同的方式驱动器吧。



第一个问题是最例子硬编码到具体的部分,我真的觉得属性值应该来自一个数据库。我认为这个想法是送我的选择署长(从数据源),并具有生成器创建一个基于我的数据定制产品。



第二个问题是我希望建设者方法实际创建零件,然后将其分配到产品,无法通过字符串,但真正的强类型的产品部件。



例如,我想创建一个表单对有我的一个生成器制造表单域,包括一个标签,一个输入部分,验证等,这样我可以阅读我的ORM的对象,检查对象的元数据,这传递给我的生成器,并添加飞新创建的用户控制结果我的网页表单。



然而,每个生成器的例子,我只找到了硬编码数据,而不是传球选择从主代码到Builder和踢出了定制产品。一切似乎是一个很大的静态case语句。举例来说,如果我有三个参数,每个10的选择,我不希望建立30混凝土构建的方法,我想创建只够制造我的产品需要的属性,这可能是只有三个。



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


解决方案

我要指的是这里维基百科的文章的C#示例。




第一个问题是大多数例子中的
混凝土部件,这点我真的觉得
应该来自数据库硬
码属性值。我想
当时的想法是送我的选择到
处长(从数据源),并有
建设者创建基于我的数据定制
产品。




在这种情况下,你将不得不类实现PizzaBuilder知道如何从数据库中检索数据。你可以这样做的几种方法。



其中之一是做出HawaiianPizzaBuilder。当初始化类将查询夏威夷披萨数据库并检索该行。然后,当各种编译(x)的方法被调用,将属性设置为检索数据库行的相应字段。



另一个办法是只是让一个PizzaDatabaseBuilder,使确保当您初始化类,你通过它,你需要该类型的比萨饼行的ID。例如,而不是

  waiter.PizzaBuilder =新HawaiianPizzaBuilder(); 

您使用



  waiter.PizzaBuilder =新PizzaDatabaseBuilder(夏威夷); 




 第二个问题是我想建设者方法实际上是创建

部分,然后将其分配到产品,
未通过字符串,但是真正的强烈
类型的产品部件。




不应该是一个问题。你需要的是一个其他工厂/生成器类型的模式初始化比萨的领域。例如:



而不是

 公共覆盖无效BuildDough(){ pizza.Dough =泛出炉; } 



你会做这样的事情。



 公共覆盖无效BuildDough(){pizza.Dough =新DoughBuilder(泛出炉); } 

 公共覆盖无效BuildDough(){pizza.Dough =新PanBakedDoughBuilder(); } 



DoughBuilder可以去另一个表在数据库中正确地填写一份PizzaDough类。


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.

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.

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.

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).

解决方案

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.

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

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.

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();

You use

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.

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

instead of

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

you would do something like

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

or

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

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

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

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