2010-07-15 137 views
0

您好我是新来设计模式和道歉,如果这个问题是造成任何混淆,虽然我想以最好的方式来描述问题。我已经实现了示例抽象工厂模式的WinForms。前端包含两个复选框来创建对象。注意:如果同时选中该复选框,则会创建这两个对象。 我正在使用objs.CreateProduct(Maxima,Ultima)方法并传递布尔值来创建对象。在这里,我传递了这两个属性的值,无论我是否想要为ultima或maxima创建对象。你能否提出其他更好的方法来实现这一目标?如果我正在创建对象,我不想传递maxima和ultima的属性。抽象工厂设计模式c#

public partial class Form1 : Form 
    { 
     public bool Maxima 
     { 
      get; 
      set; 
     } 

     public bool Ultima 
     { 
      get; 
      set; 
     } 


     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      Factory[] obj = new Factory[2]; 
      obj[0] = new B(); 
      obj[1] = new C(); 

      foreach (Factory objs in obj) 
      { 
       iProduct prod = objs.CreateProduct(Maxima,Ultima); 
       if (prod != null) 
       { 
        prod.GetDetails(); 
       } 
      } 

     } 

     private void checkBox1_CheckedChanged(object sender, EventArgs e) 
     { 
      if (checkBox2.Checked) 
       Maxima = true; 
       else 
       Maxima = false; 

      if (checkBox1.Checked) 
       Ultima = true; 
      else 
       Ultima = false; 
     } 
    } 


    abstract class Factory 
    { 
     public abstract iProduct CreateProduct(bool maxima, bool ultima); 
    } 

    class B : Factory 
    { 
     public override iProduct CreateProduct(bool maxima,bool ultima) 
     { 
      if (ultima) 
      { 
       return new NissanUltima(); 
      } 
      else return null; 
     } 
    } 

    class C : Factory 
    { 
     public override iProduct CreateProduct(bool maxima,bool ultima) 
     { 
      if (maxima) 
      { 
       return new NissanMaxima(); 
      } 
      else return null; 
     } 
    } 

    interface iProduct 
    { 
     void GetDetails(); 
    } 

    class NissanUltima:iProduct 
    { 

     public void GetDetails() 
     { 
      MessageBox.Show("NissanUltima is created"); 
     } 

    } 

    class NissanMaxima:iProduct 
    { 
     public void GetDetails() 
     { 
      MessageBox.Show("NissanMaxima is created"); 
     } 
    } 

回答

0

一个工厂基类的接口应该允许客户创建任何类型的后代实例,仅基于提供给其create方法的参数。整个观点是将对象创建与特定具体类型的知识分离,以便允许依赖注入。

如果你想为不同的后代工厂提供不同的初始化数据,这些数据应该包含在工厂类本身中或提供给工厂类本身(因为任何代码创建和配置工厂是应该知道具体的唯一部分类型)。因此,用UltimaC的bool值初始化B,其值为Maxima

坦率地说,你可能已经编辑过你的例子太多了:我不确定你想要做什么。如果WinForms代码不应该意识到具体的类型,那么您需要在它和您的工厂创建代码之间引入某种解耦接口,以便传递初始化数据。

1

我建议重新设计该代码。抽象工厂是在你的样本中创建一个抽象产品,例如汽车。一个特定的工厂添加了产品的一个特点。比方说Nissanfactory和Fordfactory 然后在每个CreateFactory()中,您可以对您想要创建的汽车模型进行描述。

abstract class Factory 
{ 
    public abstract iProduct CreateProduct(int Model); 
} 

class NissanFactory : Factory 
{ 
    public override iProduct CreateProduct(int Model) 
    { 
     // say 1 is Maxima 
     //say 2 is Untima 
     if (Model ==1) 
     { 
      return new NissanMaxima(); 
     } 
     if(Model ==2) 
     { 
      return new NissanUltima(); 

     } 
     return null; 
    } 
} 

class FordFartory : Factory 
{ 
    public override iProduct CreateProduct(int Model) 
    { 
     if (Model == 1) 
     { 
      return new GrandTorino(); 
     } 
     if (Model == 2) 
     { 
      return new Mustang(); 

     } 
     return null; 
    } 
} 

//

private void button1_Click(object sender, EventArgs e) 
    { 


     Factory[] obj = new Factory[1]; 
      obj[0] =new NissanFactory(); 

     private List<iProduct> products = new List<iProduct>(); 

     //create maxima if it's chacked 
     if (checkBox2.Checked) 
      products.Add(obj.CreateProduct(1)); 

     //create ultima 
     if (checkBox1.Checked) 
      products.Add(prod = obj.CreateProduct(2)); 

     //now you can navigate via list of created products 
     foreach (IProduct car in products) 
     { 


       prod.GetDetails(); 

     } 

    } 
+0

我可以理解,但我想在点击按钮创建类(仅当复选框被选中)的对象,这就是为什么我已经使用循环并更改checkchanged上的属性。你能否建议在你的情况下这是否可行? – user359562 2010-07-15 10:05:16

+0

@ user359562查看我的更改 – Arseny 2010-07-15 10:26:07

+0

感谢Arseny为您提供快速响应。 – user359562 2010-07-15 11:17:53