Make an abstract class or use a processor?

Posted by Tim Murphy on Stack Overflow See other posts from Stack Overflow or by Tim Murphy
Published on 2010-06-09T00:08:50Z Indexed on 2010/06/09 0:12 UTC
Read the original article Hit count: 167

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about interface