What design pattern or anti-pattern?

I will describe what I am trying to do, and hopefully someone can tell me which design template this is or indicate the best alternative.

I have a method that does a bunch of complex things that involve approximation. You can calculate the result without approximation, but this is due to more work.

I want to make some comparisons related to the inner workings of an implementation. My idea is to pass in an object that will do this extra work and save the comparison information.

I think I want to go from:

class Classifier(object): ... def classify(self, data): # Do some approximate stuff with the data and other # instance members. 

to

 class TruthComparer(object): def compute_and_store_stats(self, data, accessory_data): # Do the exact computation, compare to the approximation, # and store it. def get_comparison_stats(self): # Return detailed and aggregate information about the # differences between the truth and the approximation. class Classifier(object): ... def classify(self, data, truth_comparer=None): # Do some approximate stuff with the data and other # instance members. # Optionally, do exact stuff with the data and other # instance members, storing info about differences # between the exact computation and the approximation # in the truth_comparer if truth_comparer is not None: truth_comparer.compute_and_store_stats(data, [self._index, self._model], intermediate_approximation) 

The reason I don’t want to do these comparisons inside the classify method is because I don’t think it matches the task of this method or object to perform these comparisons.

So, what design template, if any, is this? Can you suggest an alternative?

+4
source share
2 answers

You can use Decorator Pattern . You define the Classifier interface and use the TruthComparerDecorator , which inherits from Classifier . A TruthComparer , the decorator takes a Classifier input, computes an approximation with this instance of the classifier, and then runs the compute_and_store_stats method. Using this template, you do not need to know the TruthComparer classifier. In the end, TruthComparer is a Classifier , but it does a few more things. In Java, it might look like this:

 public interface Classifier { void classify(Data data); } public abstract class TruthComparer implements Classifier { private Classifier classifier; public TruthComparerDecorator(Classifier classifier) { this.classifier = classifier; } public void classify(Data data) { classifier.classify(data); computeAndStoreStats(data); } public abstract void computeAndStoreStats(Data data); } 
+2
source

My problem with the proposed change is that it does not seem correct that the classifier requests a part of the calculation and storage of statistics. The classifier should not worry about this. Ideally, he should not even know that TruthComparer exists.

I would suggest that you really need two methods in Classifier: classify / classify_exact. Classify returns an approximate result; classify_exact returns the exact result. Instead of passing the TruthComparer parameter as a parameter, give TruthComparer two classifications and let it do its job.

Thus, you reduce the number of objects that your classifier deals with (lower link), and I think it does what happens more clearly.

0
source

All Articles