How to implement the Strategy design pattern?

172 views Asked by At

I am working on a recommendation system. It would be an Android application in which user will input their preferences and on the bases of those preferences, other matched profiles would be shown to that user. I am getting data from the user and storing it in the Firebase.

These are the numerical values and in order to show the matched profiles to that user, I am using two algorithms for calculating the similarity count between the users: Cosine similarity and Pearson correlation

I am fetching the name of the algorithm from the application and then performing the algorithm in order to show similar profiles to the user.

if (request.query.algo === "cosine") {
  // compute cosine value
} 
else if (request.query.algo === "pearson-correlation") {
  // compute pearson correlation coefficents

}

As it would be a real time application so this method is totally wrong, I want to implement Strategy design pattern where the algorithm could be decided on the run time rather than on the compile time.

So now the problem is, In Strategy design pattern, how would I decide when to use which algorithm?

For example, when you buy something with a credit card, the type of credit card doesn't matter. All credit cards have a magnetic strip that has encoded information in it. The strips, and what it contains, represent the 'interface' and the type of card would be the 'implementation'. Each credit card can be replaced by any other and all are fully independent of each other.

Similarly, On what bases I should choose in between Cosine and Pearson on run time with Strategy design pattern?

1

There are 1 answers

6
Vardaan Sharma On

From my understanding of it, Pearson would perform worse in those cases where two user profiles have very differing set of items (in this case the preferences).

Perhaps that could be your criteria? In cases where the number of matching preferences is above a certain threshold use Pearson and for other cases use cosine.

You could perhaps show your user a CLOSE match list, which uses cosine to show users whos profiles have a lot in common.

Then you could show a second list, which says You might also be interested in, which uses Pearson to show matching profiles who dont have a lot of common preferences.