【问题标题】:Troubles with Factory Design Pattern in Visual StudioVisual Studio 中工厂设计模式的问题
【发布时间】:2012-11-04 13:48:10
【问题描述】:

我在实现工厂设计模式时遇到了麻烦。我看过 http://dotnet.dzone.com/articles/design-patterns-c-factory, 它非常接近我的设计,因为它是我不久前发现的第一个好例子。
我看了看 Abstract Factory Design Pattern, 但我的设计非常不同,我无法决定是否需要它。

现在,这就是它的样子。以前比较简单,和我的 CR5、interface、factory、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 语句和 cmets 只会使列表变得混乱。如果你一开始就不让它可读,可能没有人会花时间阅读你的代码。
  • 那么.....这里有什么问题??
  • 哦,它可能更适合codereview.stackexchange.com 反正
  • 如前几段所述,问题是“现在我遇到了一些编译问题,我不知道该怎么办。”这是因为我将我的项目拆分为更多的 VS 项目,以便为我的 CR6 实施做好准备。有人对我看到的错误有任何建议吗?见**。
  • 我摆脱了 using 语句。我不确定问题是否与工厂实现有关,所以这就是我展示这么多代码的原因。

标签: c# visual-studio-2010 factory-pattern


【解决方案1】:

我的猜测是您的各种项目引用了不同版本的 .Net 运行时或平台配置文件。我的怀疑是你的 CR5 项目是要检查的。工厂正在返回消费者的目标框架无法使用的对象。 (即 CR5 是 .Net 4,而 Factory/Consumer 是 .Net 3.5/2 或 ClientProfile - 尽管最后一个可能有效,但我并没有真正混淆不同的项目类型。)

【讨论】:

  • 感谢史蒂夫·派的建议。我知道 CR5 是比我的 El 和其他 VS 项目更旧的版本。到目前为止,这还不是问题(在我拆分成不同的 VS 项目以准备在工厂中执行 CR6 之前),但是当它以这种方式拆分时,它可能不起作用。我试试看。
【解决方案2】:

问题是我在接口参数和 CR5 项目中都引用了同一个类。由于它是循环的,因此会导致奇怪的编译错误。这发生在我搬家准备工厂使用 CR6 类时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2011-11-20
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多