简体   繁体   English

如何确定要使用哪个抽象类的实现?

[英]How to determine which implementation of an abstract class to use?

What would be the best way to determine which implementation of an abstract class to call based on a certain variable or defining feature? 确定基于某个变量或定义特征来调用抽象类的哪种实现的最佳方法是什么?

Code Example: 代码示例:

public abstract class FileProcesser
{
    private string _filePath;

    protected FileProcessor(string filePath)
    {
        if (filePath == null)
        {
            throw new ArgumentNullException("filePath");
        }

        // etc etc

        _filePath = filePath;
    }

    // some example methods for this file
    public abstract int GetFileDataCount();
    public abstract IEnumerable<FileItem> GetFileDataItems();
}

// specific implementation for say, a PDF type of file
public class PdfFileProcesser : FileProcessor
{
    public PdfFileProcessor(string filePath) : base(filePath) {}

    // implemented methods
}

// specific implementation for a type HTML file
public class HtmlFileProcessor : FileProcessor
{
    public HtmlFileProcessor(string filePath) : base(filePath) {}

    // implemented methods 
}

public class ProcessMyStuff()
{
    public void RunMe()
    {
        // the code retrieves the file (example dummy code for concept)
        List<string> myFiles = GetFilePaths();

        foreach (var file in myFiles)
        {
            if (Path.GetExtension(file) == ".pdf")
            {
                FileProcessor proc = new PdfFileProcessor(file);
                // do stuff
            }
            else if (Path.GetExtension(file) == ".html")
            {
                FileProcessor proc = new HtmlFileProcessor(file);
                // do stuff
            }
            // and so on for any types of files I may have
            else 
            { 
                // error
            }
        }
    }
}

I feel as though there is a "smarter" way to do this with the use of better OO concepts. 我觉得好像有一种“更好”的方法可以通过使用更好的OO概念来做到这一点。 The code I wrote up is an example to demonstrate what I am trying to understand, sorry if there are simple errors but the basic idea is there. 我编写的代码是一个示例,目的是演示我试图理解的内容,如果有简单的错误,但是有基本的想法,对不起。 I know this is a specific example, but I think this applies to many other types of problems too. 我知道这是一个特定的示例,但是我认为这也适用于许多其他类型的问题。

I would recommend using a factory to retrieve your processor: 我建议使用工厂来检索您的处理器:

    foreach (var file in myFiles)
    {
        string extension = Path.GetExtension(file);
        IFileProcessor proc = FileProcessorFactory.Create(extension);
        // do stuff
    }

Then your factory is something like: 那么您的工厂就像:

public static class FileProcessorFactory 
{
    public static IFileProcessor Create(string extension) {
        switch (extension) {
            case "pdf":
                return new PdfFileProcessor();
            case "html":
                return new HtmlFileProcessor();
            // etc...
        }
    }
}

Note that we are using an interface, which your abstract class would inherit from. 请注意,我们使用的是您的抽象类将继承的接口。 This allows any of your inheriting types to be returned. 这允许您返回任何继承类型。

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

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