简体   繁体   English

如何实施策略设计模式?

[英]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. 这将是一个Android应用程序,其中用户将输入他们的首选项,并根据这些首选项向该用户显示其他匹配的配置文件。 I am getting data from the user and storing it in the Firebase. 我正在从用户那里获取数据并将其存储在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 这些是数值,为了向用户显示匹配的配置文件,我使用两种算法来计算用户之间的相似度计数: Cosine similarityPearson 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? 同样,我应该基于策略设计模式在运行时在Cosine和Pearson之间选择什么依据?

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). 根据我的理解,在两个用户配置文件具有非常不同的项目集(在这种情况下为首选项)的情况下,Pearson的表现会更差。

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. 如果匹配首选项的数量大于某个阈值,则使用Pearson;对于其他情况,请使用余弦。

You could perhaps show your user a CLOSE match list, which uses cosine to show users whos profiles have a lot in common. 您也许可以向您的用户显示CLOSE匹配列表,该列表使用余弦来向用户显示其个人资料有很多共同之处。

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. 然后,您可能会显示第二个列表,表示您可能也有兴趣,该列表使用Pearson来显示没有很多共同偏好的匹配配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM