创建一个抽象类还是使用处理器?

发布于 2024-09-05 08:58:08 字数 2718 浏览 4 评论 0原文

我正在开发一个类来比较两个目录,并在目标目录中不存在源目录中的目录/文件时运行操作。

下面是该类的原型:

public abstract class IdenticalDirectories
{

    private DirectoryInfo _sourceDirectory;
    private DirectoryInfo _destinationDirectory;

    protected abstract void DirectoryExists(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);
    protected abstract void DirectoryDoesNotExist(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);
    protected abstract void FileExists(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);
    protected abstract void FileDoesNotExist(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);

    public IdenticalDirectories(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory)
    {
        ...
    }

    public void Run()
    {

        foreach (DirectoryInfo sourceSubDirectory in _sourceDirectory.GetDirectories()) 
        {
            DirectoryInfo destinationSubDirectory = this.GetDestinationDirectoryInfo(subDirectory);

            if (destinationSubDirectory.Exists()) {
                this.DirectoryExists(sourceSubDirectory, destinationSubDirectory);
            } else {
                this.DirectoryDoesNotExist(sourceSubDirectory, destinationSubDirectory);
            }

            foreach (FileInfo sourceFile in sourceSubDirectory.GetFiles()) {
                FileInfo destinationFile = this.GetDestinationFileInfo(sourceFile);

                if (destinationFile.Exists()) {
                    this.FileExists(sourceFile, destinationFile);
                } else {
                    this.FileDoesNotExist(sourceFile, destinationFile);
                }
            }
        }
    }
}

上面的原型是一个抽象类。我想知道使该类成为非抽象类并让 Run 方法接收处理器是否会更好?例如。

public void Run(IIdenticalDirectoriesProcessor processor)
{

    foreach (DirectoryInfo sourceSubDirectory in _sourceDirectory.GetDirectories()) {
        DirectoryInfo destinationSubDirectory = this.GetDestinationDirectoryInfo(subDirectory);

        if (destinationSubDirectory.Exists()) {
            processor.DirectoryExists(sourceSubDirectory, destinationSubDirectory);
        } else {
            processor.DirectoryDoesNotExist(sourceSubDirectory, destinationSubDirectory);
        }


        foreach (FileInfo sourceFile in sourceSubDirectory.GetFiles()) {
            FileInfo destinationFile = this.GetDestinationFileInfo(sourceFile);

            if (destinationFile.Exists()) {
                processor.FileExists(sourceFile, destinationFile);
            } else {
                processor.FileDoesNotExist(sourceFile, destinationFile);
            }

        }

    }

}

您认为每种实施方式的优缺点是什么?

I'm developing a class to compare two directories and run an action when a directory/file in the source directory does not exist in destination directory.

Here is a prototype of the class:

public abstract class IdenticalDirectories
{

    private DirectoryInfo _sourceDirectory;
    private DirectoryInfo _destinationDirectory;

    protected abstract void DirectoryExists(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);
    protected abstract void DirectoryDoesNotExist(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);
    protected abstract void FileExists(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);
    protected abstract void FileDoesNotExist(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory);

    public IdenticalDirectories(DirectoryInfo sourceDirectory, DirectoryInfo destinationDirectory)
    {
        ...
    }

    public void Run()
    {

        foreach (DirectoryInfo sourceSubDirectory in _sourceDirectory.GetDirectories()) 
        {
            DirectoryInfo destinationSubDirectory = this.GetDestinationDirectoryInfo(subDirectory);

            if (destinationSubDirectory.Exists()) {
                this.DirectoryExists(sourceSubDirectory, destinationSubDirectory);
            } else {
                this.DirectoryDoesNotExist(sourceSubDirectory, destinationSubDirectory);
            }

            foreach (FileInfo sourceFile in sourceSubDirectory.GetFiles()) {
                FileInfo destinationFile = this.GetDestinationFileInfo(sourceFile);

                if (destinationFile.Exists()) {
                    this.FileExists(sourceFile, destinationFile);
                } else {
                    this.FileDoesNotExist(sourceFile, destinationFile);
                }
            }
        }
    }
}

The above prototype is an abstract class. I'm wondering if it would be better to make the class non-abstract and have the Run method receiver a processor? eg.

public void Run(IIdenticalDirectoriesProcessor processor)
{

    foreach (DirectoryInfo sourceSubDirectory in _sourceDirectory.GetDirectories()) {
        DirectoryInfo destinationSubDirectory = this.GetDestinationDirectoryInfo(subDirectory);

        if (destinationSubDirectory.Exists()) {
            processor.DirectoryExists(sourceSubDirectory, destinationSubDirectory);
        } else {
            processor.DirectoryDoesNotExist(sourceSubDirectory, destinationSubDirectory);
        }


        foreach (FileInfo sourceFile in sourceSubDirectory.GetFiles()) {
            FileInfo destinationFile = this.GetDestinationFileInfo(sourceFile);

            if (destinationFile.Exists()) {
                processor.FileExists(sourceFile, destinationFile);
            } else {
                processor.FileDoesNotExist(sourceFile, destinationFile);
            }

        }

    }

}

What do you see as the pros and cons of each implementation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2024-09-12 08:58:08

传入 IIdenticalDirectoriesProcessor 可以为您提供动态运行时灵活性,并且可以使 IdenticalDirectories 类更易于测试,并且不会像您最初提出的抽象/派生类解决方案那样将 IIdenticalDirectoriesProcessor 的实现耦合到 IdenticalDirectories 类。

当我现在只有 1 或 2 个抽象方法要重写时,我通常只接受 Func 或 Action 委托,而不是创建接口。这为您提供了最高程度的解耦。然而,超过 1 或 2 个就会变得笨重,因此您需要选择更传统的实现。

只是我的意见,介意。

Passing in an IIdenticalDirectoriesProcessor can give you dynamic runtime flexibility and can make the IdenticalDirectories class easier to test and won't couple the implementation of the IIdenticalDirectoriesProcessor to the IdenticalDirectories class like the abstract/derived class solution you originally proposed.

When I only have 1 or 2 abstract methods to override these days I often just accept Func or Action delegates instead of creating an interface. This gives you the highest degree of de-coupling. Any more than 1 or 2 however and it becomes clunky so you'd want to choose a more traditional implementation.

Just my opinions, mind.

若水微香 2024-09-12 08:58:08

在 .NET 中,委托是实现回调的首选方式。在不支持委托的其他语言(即大多数版本的 Java)中,接口是常见的,有时类提供接口的默认实现。

在仅支持单继承的语言中,使用抽象类有一个明显的缺点:它耗尽了使用者的一个继承槽。这不是接口问题。

In .NET, a delegate is the preferred way of implementing a callback. In other languages where delegates are not supported (i.e. most versions of Java) then interfaces are common, sometimes with a class providing a default implementation of the interface.

In languages which support only single inheritance, there's a definite downside to using an abstract class: it uses up the consumer's one inheritance slot. This isn't a problem with interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文