2013-10-30 48 views
2

我有一个编程任务,我必须创建一个需要很多步骤的对象。 我决定申请Builder。我做得对吗?我正在实施Builder模式吗?

我复杂的类(在模式的产品)

public class ProductExchangePlan 
{ 
    #region FixMembers 

    [Display(Name="Ütköző")] 
    public Bumper Bumper { set; get; } 

    [Display(Name = "Behelyezőnyomás")] 
    public float InsertionPressure { set; get; } 

    [Display(Name = "Magazin")] 
    public Magazin Magazin { set; get; } 

    #endregion 

    #region SimpleTags 

    [Display(Name = "Programszám")] 
    public string ProgramNumber { set; get; } 

    [Display(Name = "Kemény és puha minta")] 
    public string HardAndSoftSample { set; get; } 

    #endregion 

    [Display(Name = "Csaphossz")] 
    public float TapLength { set; get; } 

    [Display(Name = "Beigazító fej")] 
    public AdjustmentHead AdjustmentHead { set; get; } 

    [Display(Name = "Forgásirány")] 
    public RotationDirection RotationDirection { set; get; } 

    [Display(Name = "Vizsgálótüske")] 
    public TestPin TestPin { set; get; } 

    [Display(Name = "Betétdarab")] 
    public InsertionPiece InsertionPiece { set; get; } 

    [Display(Name = "Görgők behelyezése")] 
    public Station RollerInsertionStation { set; get; } 

    [Display(Name = "Nyomórugók behlyezése")] 
    public Station SpringInsertionStation { set; get; } 

    [Display(Name = "Csereütköző")] 
    public ExchangeBumper ExchangeBumper { set; get; } 

    [Display(Name="Zsírzófej")] 
    public GreasingHead GreasingHead { set; get; } 

    [Display(Name = "Zsírzóventilátor")] 
    public GreasingVentilator GreasingVentilator { set; get; } 
} 

而这些都是实施的其他部分:

/// <summary> 
/// The 'Builder' interface 
/// </summary> 
interface IProductExchangePlanBuilder 
{ 
    ExchangePlanGeneratorViewModel Input { set; get; } 
    ProductExchangePlan ProductExchangePlan { set; get; } 

    void BuildFixMembers(); 
    void BuildSimpleTags(); 
    void BuildTapLength(); 
    void BuildAdjustmentHead(); 
    void BuildRotationDirection(); 
    void BuildTestPin(); 
    void BuildInsertionPiece(); 
    void BuildRollerInsertionStation(); 
    void BuildSpringInsertionStation(); 
    void BuildExchangeBumper(); 
    void BuildGreasingHead(); 
    void BuildGreasingVentilator(); 
} 

/// <summary> 
/// The concrete builder 
/// </summary> 
public class ProductExchangePlanBuilder : IProductExchangePlanBuilder 
{ 
    public ProductExchangePlanBuilder(ExchangePlanGeneratorViewModel input) 
    { 
     this.Input = input; 
     this.ProductExchangePlan = new ProductExchangePlan(); 
    } 

    public ExchangePlanGeneratorViewModel Input 
    { 
     get 
     { 
      ... 
     } 
     set 
     { 
      ... 
     } 
    } 

    public ProductExchangePlan ProductExchangePlan 
    { 
     get 
     { 
      ... 
     } 
     set 
     { 
      ... 
     } 
    } 

    public void BuildFixMembers() 
    { 
     ... 
    } 

    public void BuildSimpleTags() 
    { 
     ... 
    } 

    public void BuildTapLength() 
    { 
     ... 
    } 

    public void BuildAdjustmentHead() 
    { 
     ... 
    } 

    public void BuildRotationDirection() 
    { 
     ... 
    } 

    public void BuildTestPin() 
    { 
     ... 
    } 

    public void BuildInsertionPiece() 
    { 
     ... 
    } 

    public void BuildRollerInsertionStation() 
    { 
     ... 
    } 

    public void BuildSpringInsertionStation() 
    { 
     ... 
    } 

    public void BuildExchangeBumper() 
    { 
     ... 
    } 

    public void BuildGreasingHead() 
    { 
     ... 
    } 

    public void BuildGreasingVentilator() 
    { 
     ... 
    } 
} 

/// <summary> 
/// The Director 
/// </summary> 
public class ProductExchangePlanDirector 
{ 
    public void Construct(IProductExchangePlanBuilder builder) 
    { 
     builder.BuildAdjustmentHead(); 
     builder.BuildExchangeBumper(); 
     builder.BuildFixMembers(); 
     builder.BuildGreasingHead(); 
     builder.BuildGreasingVentilator(); 
     builder.BuildInsertionPiece(); 
     builder.BuildRollerInsertionStation(); 
     builder.BuildRotationDirection(); 
     builder.BuildSimpleTags(); 
     builder.BuildSpringInsertionStation(); 
     builder.BuildTapLength(); 
     builder.BuildTestPin(); 
    } 
} 

所有的BuildXX方法使用的的ExchangePlanGeneratorViewModel Input财产具体的建设者,并要求在数据库中查询。

所以我的问题是,这是一个很好的执行这项任务?你怎么能改进它?

+0

难道你不喜欢单一的“构建”功能?在这种情况下,装饰者模式似乎更适合我。除非你的所有物体都有测试针的标头等。 – Stefan

+0

@Stefan对象始终具有所有属性。只是值取决于用户输入的值。单个Build函数太长。 – jannagy02

+0

好吧,它看起来像你遵循所有的规则:),在这种情况下,你很好去。我通常会尽量避免这种模式,因为它有很多责任,所以最终可能会产生大量的代码文件,而且这些文件很难维护。但这可能只是个人意见。 – Stefan

回答

0

这里有3个问题,你的设计,你应该看看:

  1. 你建设者的客户端(ProductExchangePlanDirector)不 以增量方式配置建设者所以没有 需要IProductExchangePlanBuilder到成为这样一个健谈的界面。它很容易忘记在客户端代码中调用所需的 Build...方法之一。

  2. 您的构建器类使用公共访问器公开通过属性 构建的对象。这意味着客户端代码可以检索ProductExchangePlan的部分完成的实例,这是构建器模式应该解决的问题中的一个 。 IProductExchangePlanBuilder应改为有Build方法 将返回的 ProductExchangePlan完整构建和有效的实例,或者抛出一个异常,如果不是全部需要 数据是从它的 ExchangePlanGeneratorViewModel实例。

  3. 在你的设计,ProductExchangePlanBuilder似乎 负责从视图模型都构建ProductExchangePlan的 依赖关系,构建从自身 ProductExchangePlan想必一些验证 (除非视图模型分析用户输入和构建 依赖关系本身)。如果是这样的话,你可能要进一步 独立的这些责任,由具有从视图模型 ProductExchangePlanDirector解析输入 构建依赖关系,并呼吁 Build它之前配置的建设者。

+0

感谢您的回复。只有一点你的观点:我是在dofactory.com上撰写的(http://www.dofactory.com/Patterns/PatternBuilder.aspx#_self1) – jannagy02

+0

@ jannagy02您链接到的文章中的示例代码不包含任何验证代码,以确保构造对象在返回到客户端代码之前是有效的。我认为他们没有利用建造者模式提供的机会来确保不需要构建无效的半建物体,这是他们的例子中的一个弱点。 我更喜欢只在构建器完全配置了所有必需的依赖关系时才调用构造器。 – Ergwun