What is the best way to represent priorities / preferences in objects?

What is the best way to model priorities / preferences in a domain?
For instance. suppose I have a Person class representing a person and has some preferences, for example. PersonX loves pizza, spaghetti and omelete, and in fact he would prefer spaghetti over pizza.
How are they best modeled? The first thought was to simply put them in a List (as a member variable of Person ), preserving the preference according to the insertion order, for example. the first on the list is the most preferred, then the second on the list the second most preferred, etc., but it seems to me that this is too tiring. For instance. when I want to do searches or associations for preferences, etc.
Perhaps there is a standard approach for such problems?

+6
source share
4 answers

You can use the list as you suggested and add a method to your Person class that returns a Comparator<Preference> that can compare 2 preferences for this person:

 Person somebody.addPreference(pizzaPreference); Person somebody.addPreference(omelettePreference); .... Comparator<Preference> c = somebody.getPreferenceComparator(); boolean prefersPizzaOverOmelette = (c.compare(pizzaPreference, omelettePreference) > 0); boolean hasNoPreferenceBetweenPizzaAndOmelette = (c.compare(pizzaPreference, omelettePreference) == 0); 

And the comparator will simply check the preference index in your list (if it contains that preference, etc.).

+1
source

You can use the priority queue to represent the priorities of objects and determine the appropriate Comparator for your class that takes into account the described rules. The queue uses a bunch of priorities that will take care of saving objects sorted by priority as they are inserted.

+1
source

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

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

Then, let your users sort their settings (hence the ranks column) and the order in this property / column if necessary.

EDIT

Looking at it again, I want to redefine my class. The preference should be the same as the user over the other, for example ...

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

This new class defines what a person likes, and it allows you to assign a rank so that when you have many instances of this class (or its data representation), they can be ranked, which affects them, creates preference, because preference is this is essentially a user who likes something over something else. This in itself is not a preference, because it does not compare with anything else.

This approach allows you to evaluate n interests against each other, creating a large collection of preferences.

+1
source

Preference as a whole is a complete Entity in its own way, although it does not matter if it is not associated with any Person , but can live on its own.

Regarding priority priority, Preference does not have any priority omelet, pizza is the same, but it has priority when it is associated with a person (for example, I like pizza more than omelet, it doesn’t depend on me for pizza or omelet).

So you have a Preference and Person object.

Now the preference will be different from Person to Person, so you will associate Preference with Person.

Since priority is very dependent on the Person, you have several options to achieve:

  • Use PriorityQueue - When adding priority, assign priority. This queue will be present in the Person class.
  • Use custom comparator (as suggested by @assylias)
  • Add rules for preference (based on location, gender, etc) and pre-configured rules that, given specific criteria, will return the preference calculated by the System, although they have a manual override of this. In this case, you can simply use PriorityQueue .
+1
source

Source: https://habr.com/ru/post/914638/


All Articles