简体   繁体   中英

Best way to connect classes in an hierarchy fashion?

I'm really unsure if I'm doing this right.

I have a Database which consists of a table which has these "parameters"

Filepath
Artist
Album
Track
Year
Duration
Tracknumber
Genre

I want to read from the database and fill them in a list of objects.

What I've done (which I'm unsure if it's the right thing to do) is to have a list of Artist class, which has a name, and a List of Albums.

The album object has a Name, Year and a List of Tracks.

The Track objects has Name, Duration, Genre, Year and Filepath.

It's basically a tree of lists, where the Artists opens up a branch of Albums, and the Albums opens up another branch of Tracks.

Is this the best method of setting things up?

Hope you understand what I'm trying to say. Thank you.

Is this the best method of setting things up?

Depends on your interpretation of what's 'best'.

Seems to be you've relational data that may be represented in hierarchical form (or have an hierarchy-based view) whose main key is the Artist , like this:

  • Artist
    • Albums
      • Tracks
        • Year

But your data may have tracks (or whole albums) that may have been created by two or more artists, for example. Your structure may as well be presented like this:

  • Year
    • Albums
      • Artists

IMHO this kind of data could be better served in a relational structure, like this:

  • Artist Class
    • Name , string property
    • Albums , collection of Album objects
    • Tracks , collection of Track objects
  • Track Class
    • Name , string property
    • Year , int property
    • Artists , collection or Artist objects
    • Album , Album object reference (since a track belongs to a single album)

And so forth. That would even help the dynamic creation of different hierarchical views.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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