繁体   English   中英

来自通用接口的工厂模式

[英]Factory pattern from generic interface

我有通用接口IProcessor<T, TU>及其2个实现:

StandartDocumentProcessorModDocuemntProcessor

我将使用Factory模式(或类似的模式)实例化变量currentProcessor

IProcessor<T, TU> currentProcessor = Factory.Create(argument)

然后,我将调用方法currentProcessor.ProcessFrom()currentProcessor.ProcessTo()

第一个问题 :currentProcessor不能具有IProcessor<T,TU>IProcessor类型,因为CLR不允许这样做,并且坚持为变量指定特定类型。

第二个问题 :我上网冲浪,发现使用ReflectionActivator.CreateInstance()丑陋解决方案性能降低有熟悉的问题。

请注意,是否可以避免:反射,投射到object或使用dynamic

UPDIProcessor.cs

public interface IProcessor<T, TU> where T : DocumentValidation
    {
        HtmlContent RetrieveDocument(SearchItem item);

        bool ValidateObject(T obj);

        T ProcessFrom(HtmlContent content);

        TU ProcessTo(T source);

        bool SaveTo(TU target, T source);

    } 

首先实施的IProcessor - StandardDocumentProcessor.cs

class StandardDocumentProcessor : IProcessor<Document, XmlDocument>
    {
        public HtmlContent RetrieveDocument(SearchItem item)
        {
            throw new NotImplementedException();
        }

        public bool ValidateObject(Document obj)
        {
            throw new NotImplementedException();
        }

        public Document ProcessFrom(HtmlContent content)
        {
            throw new NotImplementedException();
        }

        public XmlDocument ProcessTo(Document source)
        {
            throw new NotImplementedException();
        }

        public bool SaveTo(XmlDocument target, Document source)
        {
            throw new NotImplementedException();
        }
    }

IProcessor第二种实现ModDocumentProcessor.cs

public class ModDocumentProcessor : IProcessor<ModDocument, XmlDocument>
    {
        public HtmlContent RetrieveDocument(SearchItem item)
        {
            throw new NotImplementedException();
        }

        public bool ValidateObject(ModDocument obj)
        {
            throw new NotImplementedException();
        }

        public ModDocument ProcessFrom(HtmlContent content)
        {
            throw new NotImplementedException();
        }

        public XmlDocument ProcessTo(ModDocument source)
        {
            throw new NotImplementedException();
        }

        public bool SaveTo(XmlDocument target, ModDocument source)
        {
            throw new NotImplementedException();
        }
    }

现在,我将创建工厂模式以使用某些特定的类实例化IProcessor<T, TU>

????? currentProcessor = Factory.Create(input_argument_which_allows_to_define_concrete_implementation_of_IProcessor)

这与在此处显示Create方法的实现无关。 我问的只是返回Create方法的类型

C# 4.0您可以简单地将工厂方法声明为动态方法,它将起作用:

public static dynamic Create(string at) {
   if (condition)
     return new StandardDocumentProcessor();
   else
     return new ModDocumentProcessor();
}

调用:

dynamic m = Create("mod");

当然,这就像作弊一样,因为通过dynamic使用,您将失去类型安全性(增加运行时异常),并且会降低性能。

如果没有你的应用程序域的想法,但如果这方面是导入你的,我会改变一些事情,并尝试仿制out的参数。

public interface IProcessor<out T, out TU> where T : DocumentValidation {
    HtmlContent RetrieveDocument(SearchItem item);
    bool ValidateObject(DocumentValidation obj);
    T ProcessFrom(HtmlContent content);
    TU ProcessTo(DocumentValidation source);
}

厂:

public static IProcessor<DocumentValidation, ProcessResult> Create(string at) {
  if (condition(at))
    return new StandardDocumentProcessor();
  else
    return new ModDocumentProcessor();
  }

请注意,我删除了SaveTo方法,因为它不适合此模式。 仍然需要时将其移动到另一个界面。 ProcessResultXmlDocument的基类/接口。

还要注意一点:如果您根本不使用泛型并使用继承/基类,那么一切都会变得更加简单。 工厂模式通常用于隐藏和封装实现细节和类型,包括一些通用参数。 如果要从工厂获取类型信息,使用工厂不是最好的主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM