What is a good design pattern for the following situation?

Ufuk Can Bicici

I have different string distance metric implementations at hand (all of them are in C#) for example, Levensthein, NeedlemanWunsch, Jaccard, etc. The job of these are same basically; take two strings as input and return a similarity score in the range [0,1]. So, I planned to make these classes to implement the same basic interface, as in the following:

public interface IStringDistanceMetric
{
    //Return a similarity between 0 and 1.
    double CompareSimilarity(string strA, string strB);
}

Each of my metrics will implement this method. But there are some metrics which will directly operate on two strings, without any other input and there are some metrics which needs some extra parameters( like the penalty given for a gap in one of the input strings, etc.). In general, I can give these parameters either in the constructor stage or just before calculating the similarity score.

My question is, in general, what would be a good design practice in order to handle such differences between the concrete distance classes? I aim that the client code which wants to use any distance metric should be oblivious to any underlying implementation detail, after it has decided use a specific kind of metric. The most obvious way would be to implement something like:

  IStringDistanceMetric metric;
  if(metricType == Metric.NeedlemanWunsch)
  {
    metric = new NeedlemanWunsch(parametersNW);
  }
  else if(metricType == Metric.Levensthein)
  {
    metric = new Levensthein(parametersL);
  }
  .
  . 
  .

But this doesn't look a good solution to me. I am kind of surprised to find myself stuck at such a basic looking design problem. Any help would be appreciated.

Bas

I guess this is highly dependant on how it is used; are the values of these parameters fixed or should they be provided when doing the comparison? If the number of possible values for these parameters is very low, the design used in .NET for StringComparers could be followed (courtesy of sourceof.net):

private static readonly StringComparer _invariantCulture = new CultureAwareComparer(CultureInfo.InvariantCulture, false);        
private static readonly StringComparer _invariantCultureIgnoreCase = new CultureAwareComparer(CultureInfo.InvariantCulture, true);      
private static readonly StringComparer _ordinal = new OrdinalComparer(false);
private static readonly StringComparer _ordinalIgnoreCase = new OrdinalComparer(true);        

public static StringComparer InvariantCulture { 
    get {
        return _invariantCulture;
    }
}

If you are getting user input or configuration settings to specify your parameter values, and they vary each time you compare strings, a factory method would probably be an appropriate solution, the 'most obvious way' presented in your answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is a good class design for node-connection situation?

From Dev

What is a good design pattern for using an abstract superclass?

From Dev

What is a good design pattern for tracking issues in a class?

From Dev

Situation based design pattern application

From Dev

What is a good java design pattern for similar objects with branching logic

From Dev

What Design Pattern can I use to accomplish the following

From Dev

Which will be the better database design in following situation? Why?

From Dev

Design pattern to handle following scenario

From Dev

what the difference between following situation in iOS?

From Dev

What is a good design pattern for dealing with complex classes that have potential null sub classes?

From Dev

What is Facet design pattern?

From Dev

Design Patterns - What pattern is this?

From Dev

What is the name for this design pattern?

From Dev

What is the benefit of this design pattern?

From Dev

What is the Hexagon design pattern

From Dev

What is this design pattern called?

From Dev

What is the design pattern?

From Dev

What is Facet design pattern?

From Dev

What is this design pattern called?

From Dev

What pattern to use for the following scenario?

From Dev

What pattern to use for the following scenario?

From Dev

What is good for design administrative website?

From Dev

Good practice design pattern for Exception handling

From Dev

A good design pattern for documenting every operation

From Dev

Generic DAO design-pattern with inheritance. is this a good design?

From Dev

Generic DAO design-pattern with inheritance. is this a good design?

From Dev

What does this design pattern accomplish?

From Dev

What design pattern is appropriate here?

From Dev

What is the name of this inheritance design pattern?

Related Related

  1. 1

    What is a good class design for node-connection situation?

  2. 2

    What is a good design pattern for using an abstract superclass?

  3. 3

    What is a good design pattern for tracking issues in a class?

  4. 4

    Situation based design pattern application

  5. 5

    What is a good java design pattern for similar objects with branching logic

  6. 6

    What Design Pattern can I use to accomplish the following

  7. 7

    Which will be the better database design in following situation? Why?

  8. 8

    Design pattern to handle following scenario

  9. 9

    what the difference between following situation in iOS?

  10. 10

    What is a good design pattern for dealing with complex classes that have potential null sub classes?

  11. 11

    What is Facet design pattern?

  12. 12

    Design Patterns - What pattern is this?

  13. 13

    What is the name for this design pattern?

  14. 14

    What is the benefit of this design pattern?

  15. 15

    What is the Hexagon design pattern

  16. 16

    What is this design pattern called?

  17. 17

    What is the design pattern?

  18. 18

    What is Facet design pattern?

  19. 19

    What is this design pattern called?

  20. 20

    What pattern to use for the following scenario?

  21. 21

    What pattern to use for the following scenario?

  22. 22

    What is good for design administrative website?

  23. 23

    Good practice design pattern for Exception handling

  24. 24

    A good design pattern for documenting every operation

  25. 25

    Generic DAO design-pattern with inheritance. is this a good design?

  26. 26

    Generic DAO design-pattern with inheritance. is this a good design?

  27. 27

    What does this design pattern accomplish?

  28. 28

    What design pattern is appropriate here?

  29. 29

    What is the name of this inheritance design pattern?

HotTag

Archive