繁体   English   中英

类设计C#铸造

[英]Class Design C# Casting

假设我有一个程序可以将文本写入文件中(不是真的,但是更容易解释)。 我希望为每个文件类型(例如从FileTypeMaster继承的PdfType和WordType)使用单独的类。

FileTypeMaster (base)
-PdfType : FileTypeMaster
-WordType : FileTypeMaster (same methods as pdftype but works different)

现在要解决真正的问题...我希望用户决定在programstart上使用哪种类型。 如果他想要Pdf或Word,则该方法调用应该看起来相同(因为word是新的,并且该程序之前仅用于pdf)。

例如,如何使用pdf进行工作:

static FileTypeMaster MyFavoriteType; //declare a general var 
MyFavoriteType = new PdfType(); //cast general var to the wanted type
MyFavoriteType.CompileThis();

它如何与单词一起使用:相同,但MyFavoriteType = new WordType();`

为此而设计了接口。 MyFavoriteType应该是一个接口,内置任何您将需要的东西(功能和专有属性)。然后每个想要的类型将实现该接口并做自己的事情。

这是一个小例子:

public interface Polygon
{
  float GetArea();
}
public class Square : Polygon
{
  float Side;
  public Square(float side)
  {
    Side = side;
  }
  Polygon.GetArea()
  {
    return side*side;
  } 
}

您现在可以执行以下操作:

Polygon MyPolygon = new Square(5f);
float area = MyPolygon.GetArea();

这将适用于正确实现Polygon接口的任何单个类。 因此这是可能的:

Polygon MyPolygon = new Circle(5f);
float area = MyPolygon.GetArea();

在您的情况下,界面如下所示:

public interface MyFavoriteType//or name it FileTypeMaster, since that's what you want to anme it
{
  void CompileThis();
}

仅当在子类之间共享行为时才使用基类。 如果您仅共享方法签名和属性(例如,每个子类实际上并不使用任何基类功能),请改用接口。

您要在这里做的就是利用多态性 实际上,您可以在这里实现策略设计模式 ,那里有一个基类(或接口)

public abstract class FileTypeMaster
{
    public abstract CompileThis();
}

对于Word和Pdf,具有不同的实现:

public class PdfType : FileTypeMaster
{
    public override CompileThis () {}
}

然后,在您的调用程序中,您可以创建正确的实现:

FileTypeMaster x = new PdfType();
x.CompileThis()

要么

FileTypeMaster x = new WordType() x.CompileThis()

实际上,您可以使用工厂类或工厂方法将初始化抽象化:

public abstract class FileTypeMaster
{
    public abstract CompileThis();

    public static FileTypeMaster Create( FileType type )
    {
         switch( type )
         {
             case FileType.Word : return new WordType();

             case FileType.Pdf : return new PdfType();

             default:
                 throw new NotImplementedException();
         }
    }
}

要添加到其他帖子中,您通常会拥有一个工厂,该工厂给定一个指示符(例如代表用户选择的文件类型的枚举值)生成​​适当类的实例,并将其作为接口类型返回。

您的基本类型可以是类或接口。 假设它是一个接口:

interface IFileProcessor { void CompileThis(); }
class PdfProcessor : IFileProcessor { public void CompileThis() { /* ... implementation omitted ... */ }
class WordProcessor : IFileProcessor { public void CompileThis() { /* ... implementation omitted ... */ }

然后:

private enum ProcessorType
{
    Undefined,
    Word,
    Pdf
}

public void Main(string[] args)
{
    ValidateArgs(args); // implementation omitted
    ProcessorType requestedProcessor = GetRequestedProcessorFromArgs(args); // implementation omitted
    IFileProcessor processor = GetProcessor(requestedProcessor);
    processor.CompileThis();
}

private IFileProcessor GetProcessor(ProcessorType requestedProcessorType)
{
    switch (requestedProcessorType)
    {
        case ProcessorType.Pdf:
            return new PdfProcessor();
        case ProcessorType.Word:
            return new WordProcessor();
        default:
            throw new ArgumentException("requestedProcessorType");
    }
}

暂无
暂无

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

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