我在實現工廠設計模式時遇到問題。我看過 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)
{
…
}
}
.
.
.
}
請將代碼縮減爲理解問題所需的部分。特別是'using'語句和評論只會使列表混亂。可能沒有人會花時間閱讀你的代碼,如果你不把它放在首位。 –
所以.....這裏有什麼問題? – D3vtr0n
哦,它可能更適合http://codereview.stackexchange.com/ –