2012-11-15 44 views
-1

我在實現工廠設計模式時遇到問題。我看過 http://dotnet.dzone.com/articles/design-patterns-c-factory, ,它非常接近我的設計,因爲它是我發現的第一個很好的例子。
我看了看 Abstract Factory Design Pattern, 但我的設計非常不同,我無法決定是否需要該設計。Visual Studio中的工廠設計模式出現問題

現在,這就是它的樣子。它曾經更簡單,與我的CR5,界面,工廠,CB_spec,El等在同一個visual studio項目中。我必須按照設計討論中所示的方式移動,並且需要將CR6等分開。現在我收到了一些編譯問題,我不知道該怎麼處理。見下文。我的問題是關於下面的兩個編譯問題。

我ICR Visual Studio項目:

public interface iCR 
    { 
     int CB_IO_Init(int slaveIndex); 
     int WritePortReady(); 
     int WritePortBusy(); 
     void initCRData(byte[] writeBuffer, byte[] statusBuffer, int SlaveIndex, USB_Comm.CB cb, int cr_Type); 
     int ProcessTWriting(ref Byte[] writeDat, ref Byte[] statusDat, ref Byte[] dataDumpWriteCheck); 
     void Failure(String message); 
     void Success(String message); 

    } 

我CR_Factory Visual Studio項目

namespace CR_Factory 
{ 

public class Cr 
{ 

} 

public class CRFactory 
{ 
    public enum CRType 
    { 
     CR0, 
     CR1, 
     CR3, 
     CR4, 
     CR5, 
     CR6 
    } 

    public CRFactory() 
    { 
    } 

    public iCR GetCR(CRType type) 
    { 
     iCR cr = null; 
     switch (type) 
     { 
      case CRType.CR5: 
       cr = new CR5(); //**compile error..Cannot implicitly convert type ‘CR5’ to iCR’. An explicit conversion exists (are you missing a cast?) 
       break; 
      case CRType.CR6: 
       //not done yet 
       break; 
      default: 
       throw new ArgumentException(string.Format("A CR of type {0} cannot be found", Enum.GetName(typeof(CRType), type))); 
     } 
     return cr; 
    } 

    public CRType DetermineCR_Type(int type) 
    { 
     switch (type) 
     { 
      case 0: 
       return CRType.CR0; 
      //break; 
      case 1: 
       return CRType.CR1; 
      case 3: 
       return CRType.CR3; 
      case 4: 
       return CRType.CR4; 
      case 5: 
       return CRType.CR5; 
      case 6: 
       return CRType.CR6; 
      default: 
       throw new ArgumentException(string.Format("A type of type {0} cannot be found", type)); 

     } 
    } 

    } 
} 

我CR5 Visual Studio項目有很多的班,但現在我只是展示你在工廠中提到的部分。後來,我將創建一個CR6 VS項目等:

public class CR5 : iCR 
{   


    CB_703 cb_specific = null; 

    //constructor 
    public CR5() 
    { 
     cb_specific = new CB_703(SlaveIndex); 
    } 


    public int CB_IO_Init(int SlaveIndex) 
    { 
     int result = -534; 
     result = cb_specific.IO_Init(SlaveIndex); 
     return result; 
    } 
. 
. 
. 
} 

我還有一個Visual Studio項目(實際數)來實例化廠,並得到適當的類型。我們將其稱爲El:

namespace CrWr 
{ 

public partial class PControl : UserControl 
{ 
    //setup 

    //constructor 
    public PControl() 
    { 

    } 

    /// <summary> 
    /// Get the P Control for chosen dll 
    /// </summary> 
    public Control GetPControl(USB_Comm.CB cbInstance, string dllSelected, THandlerApplication.Temp.TEMP[] temp, string dll, SC.SC.S_C c0) 
    { 
     cb = cbInstance; 
     createControls(); 
     itsDll = dll; 
     tArr = temp; 
     cert = c0; 

     CR_Factory.CRFactory factory = new CR_Factory.CRFactory(); 
     CRFactory.CRType type = factory.DetermineCR_Type(cr_Type); 
     try 
     { 
      cr = factory.GetCR(type); //**compile error GetCR is not supported by the language 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.InnerException); 
     } 
     return this; 
    } 

private void OnP() 
    { 
     int result = -536; 

     while (rL) 
     { 
      result = cr.CB_IO_Init(SlaveIndex); 
      if (result == 0) 
      { 
       … 
      } 
     } 

. 
. 
. 
} 
+2

請將代碼縮減爲理解問題所需的部分。特別是'using'語句和評論只會使列表混亂。可能沒有人會花時間閱讀你的代碼,如果你不把它放在首位。 –

+0

所以.....這裏有什麼問題? – D3vtr0n

+2

哦,它可能更適合http://codereview.stackexchange.com/ –

回答

0

問題最終是因爲我在我的界面參數和CR5項目中都參考了同一個類。由於它是循環的,所以會導致編譯錯誤。當我爲了讓工廠準備使用CR6課程而動身時,發生了這種情況。

1

我的猜測是你的各種項目引用了不同版本的.Net運行時或平臺配置文件。我的嫌疑人將是你的CR5項目是一個檢查。工廠正在返回消費者的目標框架無法使用的對象。 (即CR5是.Net 4,而工廠/消費者是.Net 3.5/2或ClientProfile - 雖然最後一個可能會起作用,但我並不真正搞亂不同的項目類型。)

+0

謝謝你的建議,Steve Py。我知道CR5比我的El和其他VS項目更舊。到目前爲止,它還沒有成爲問題(在我分拆到不同的VS項目之前,更多地準備在工廠中完成CR6),但是當它以這種方式拆分時也許不起作用。我會試一試。 – Michele