在对象中表示优先级/偏好的最佳方式是什么? [英] What is the best way to represent priorities/preferences in objects?

查看:342
本文介绍了在对象中表示优先级/偏好的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在域中优化/偏好设置的最佳方法是什么?

例如假设我有一个类 Person 代表一个人,并有一些喜好,例如 PersonX 喜欢比萨,spaggetti和omelete,其实他更喜欢spagetti超过比萨饼。

这些最好的模特儿?

第一个想法是将它们放在列表(作为 Person 的成员变量)中,保持根据插入的偏好命令例如列表中的第一个是最受欢迎的,那么列表中的第二个是第二个最受欢迎的,但这似乎在我看来最终变得太乏味了。例如。当我想要做搜索或关联的偏好等等。

或许有一个标准的方法来解决这样的问题?

What is the best way to model priority/preferences in a domain?
E.g. assume I have a class Person representing a person and has some preferences e.g. PersonX likes pizza, spaggetti and omelete and actually he would prefer spagetti over pizza.
How are these best modelled?
The first thought would be just to put them in a List (as member variable of Person) keeping the preference according to insertion order e.g. the first in the list is the most prefered, then the second in the list the second most prefered etc but this seems to me to end up being too tedious. E.g. when I want to do searches or associations for preferences etc.
Perhaps there is a standard approach for problems like this?

推荐答案

使用这样的类结构...(赦免Java ...我是C#的人) / p>

Use a class structure like this...(pardon the sloppy Java...I'm a C# guy)

public class PersonPreference
{
     public Preference preference;
     public int rank;
}

然后,允许您的用户对其偏好进行排序(因此,排名列) ,并在必要时对该资产/列进行订购。

Then, allow your users to sort their preferences (hence, the rank column), and order on that property/column when necessary.

编辑

再次,我想重新定义我的班级。一个偏好应该是一个用户喜欢与另一个人相比...

Looking at this again, I want to redefine my class. A preference should be a user's like in comparison to another like...

public class PersonLike
{
     public string like;
     public int rank;
}

这个新类定义了一个人喜欢的东西,它允许一个等级当你有这个类的许多实例(或它的数据表示)时,它们可以被排序,这在有效的情况下创建偏好,因为偏好本质上是一个喜欢某事的用户。一个人本身不是一个偏好,因为它并没有与其他任何东西进行比较。

This new class defines something a person likes, and it allows for a rank so that when you have many instances of this class (or a data representation of it), they can be ranked, which in affect, creates the preference, because a preference is essentially a user liking something over something else. A like by itself is not a preference because it is not being compared against anything else.

这种方法允许n个兴趣被排列在一起,创造出大量的偏好。

This approach allows for n interests to be ranked against each other creating a large collection of preferences.

这篇关于在对象中表示优先级/偏好的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆