vendredi 1 mai 2020

Call same API from different methods

I have a set of validators, which are used for various comparisons, such as if 2 strings are equal or not, If String A contains String B or not, If a list of strings contain substrings or not and so on... For each comparison there exists a separate method.

I need to write a sanitization module, which will clean the strings , i.e. remove special characters, use lucene analysers, etc. before the comparison and then give back the strings.

Is there any way I can directly call the same function for sanitization in all the methods ? What design patterns can be used here?

Explaining the same using an example:

compareStringsContainsSubstring(str1, str2) {
   //sanitizeStrings API should be called here 
   //comparison
}

compareStringsAreEqual(str1, str2) {
   //sanitizeStrings API should be called here 
   //comparison
}

compareListContainsSubstring(ListOfstr, str1) {
   //sanitizeStrings API should be called here 
   //comparison
}

Is there a way to avoid calling it in every method ? All these comparison validators are present in the same class. Can I use some design pattern to solve this?

Aucun commentaire:

Enregistrer un commentaire