Issue with class design to model user preferences for different classes

Posted by Mulone on Stack Overflow See other posts from Stack Overflow or by Mulone
Published on 2010-05-17T13:04:02Z Indexed on 2010/05/18 18:30 UTC
Read the original article Hit count: 209

Hi all, I'm not sure how to design a couple of classes in my app. Basically that's a situation:

  • each user can have many preferences
  • each preference can be referred to an object of different classes (e.g. album, film, book etc)
  • the preference is expressed as a set of values (e.g. score, etc).

The problem is that many users can have preferences on the same objects, e.g.:

John: score=5 for filmid=apocalypsenow 
Paul: score=3 for filmid=apocalypsenow 

And naturally I don't want to duplicate the object film in each user.

So I could create a class called "preference" holding a score and then a target object, something like:

User{ 
  hasMany preferences 
} 

Preference{ 
  belongsTo User 
  double score 

  Film target   
  Album target 
  //etc 
} 

and then define just one target. Then I would create an interface for the target Classes (album, film etc):

Interface canBePreferred{ 
  hasMany preferences 
} 

And implement all of those classes. This could work, but it looks pretty ugly and it would requires a lot of joins to work. Do you have some patterns I could use to model this nicely?

Cheers, Mulone

© Stack Overflow or respective owner

Related posts about grails

Related posts about design-patterns