samedi 2 septembre 2017

How to implement the Strategy design pattern?

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. On what bases I should choose in between Cosine and Pearson on run time with Strategy design pattern?

Aucun commentaire:

Enregistrer un commentaire