Extending the functionality of a strategy template

I am developing an application that compares files. I decided to use the Strategy design template to handle different formats, so I have something like this:

public class Report {
   CompareStrategy strategy;
   ...
}


public interface CompareStrategy {
   int compare(InputStream A, InputStreamB);
}

Then, of course, I implement a comparison method for different file formats.

Now suppose I wanted to add another method that addresses certain restrictions for comparison (for example, omit the line in the case of an Excel or csv file or omit the node in XML).

Would be better:

  • Add another way for the interface and each implementation (there are not many at the moment)
  • Create a new interface that inherits from CompareStrategy, and then implement it?

The second question: since the differences can be of different types - it would be nice to make the difference marker interface to include something like:

int compareWithDifferences(..., Iterable<Difference> differences);

, ?

+4
3

, , (, Excel csv node XML).

,

,

public abstract class XMLCompareStrategy implements CompareStrategy {

    public int compare(InputStream A, InputStreamB) {
        // several steps
        customMethod(...);
        // more steps
    }

    protected abstract ... customMethod(...);

}

, , .

+4

:

  • - . - , , , , .
  • - , .
+2

, , CompareStrategy. , compareWithDifferences() , , - .

As Jonathan said, if you can anticipate difficulties, get ready for it. In this case, I think you should prepare. In fact, it will not cost you much time to create another interface, and you will not have to reorganize later.

0
source

Source: https://habr.com/ru/post/1682718/


All Articles