开发者

OOP: How would you set up an Artist, Album & Song relationship in OOP

开发者 https://www.devze.com 2022-12-16 00:46 出处:网络
I understand the basics of inheritance but this one is confusing me. How would you go about saying: An album object has one or more artist objects

I understand the basics of inheritance but this one is confusing me. How would you go about saying:

  • An album object has one or more artist objects
  • An album object has one or more song objects

My current code only will only allow one song per ob开发者_StackOverflow社区ject:

class Song extends Album{}
class Album extends Artist{}

I'm sure i'm overlooking something important. Any thoughts?

I'm doing this in PHP


An album has one or more artist objects. Inheritance would mean an album is an artist. What you need is (EDIT) aggregation:

class Album
{
  public $artists = array(); // Array of Artist instances
  public $songs   = array(); // Array of Song instances
}

Aggregation means every Artist and Song instance may belong to other Albums. With composition, an artist or song may belong to a single Album.


As I mentioned in one of my comments, if you had to model it via OOP I would say,

class Song {
    string[] artists;
    string title;
    int genre;
}

class Album {
    Song[] tracks;
}


Don't use inheritance for this kind of relationship, for the sake of all that is fried and filled with delicious cream!

A song is not an extension of an album, it's a part of album. Likewise an album isn't an extension of an artist, it's created by an artist. Moreover a song can appear on multiple albums, and a single album can have multiple artists.

You should be using a has-a relationship instead of inheritance for this situation. For example, you might have an Album object that has an array of Song objects as a member. That would convey the idea that songs belong to albums appropriately.


Try thinking of it in terms of which objects have which properties. I.e. "The car HAS a color" or "The balloon HAS a string".

Song HAS Artist

Album HAS Songs

So your Song class would have an Artist object, and your Album class would contain a collection of Song objects.


I don't think there is any inheritance in these three classes. Is a Song an Album? No I don't think it is so Song should not inherit from Album. Is an Album an Artist? No I don't think that is either so Album should not inherit from Artist.

Instead you want to look at encapsulation. An Album references zero or more Artists. An Album contains zero ore more songs. An artist references zero or more Albums.


inheritance relationship are named "IS-A" while the relationship you're looking for is HAS-A

You need something like this:

Album
    artists: Artist[1..*]
    songs  : Song[1..*]

Where an Album HAS from 1 to many ( * ) artists ( defined by the Artist array ) and 1 album has from 1 to many ( * ) songs, ( defined by the Song array )


Favor composition over inheritance.

By what you said it should be like:

An album object has one or more artist objects. An album object has one or more song objects

class Album { Artist[] Artists; Song[] Songs; }

However that's not exactly how I envision this. I think each Album has one or more songs that is performed by one or more artists. I would do it like:

class Album { Songs[] Songs; // other album specific properties }

class Song { Artist[] Artists; // other song specific properties }

class Artist { // artist specific properties }

And I highly recommend in looking at OOD principles.


Here's my view.

Album contains Song(s) but a Song is not an Album.
Album is associated with Artist(s), but an Album is not an Artist.

You are mixing inheritance with relationships.


This sounds like a database modeling question, not one of OOP. This is because you would most likely have a database of songs artists and albums. Not a class hierarchy.


I'm not sure you're using OOP correctly. Generally, an object extends a parent when it has the same functionality of that parent, but some extra (or specific) functionality.

For example, let's use the auto industry:

class Automobile {}

class Truck extends Automobile {}
class Car extends Automobile {}

class Lexus extends Car{}

See how Lexus is the most specific, so it builds a hierarchy as it goes up?

What you are wanting is similar to a has_many relationship. An artist has many albums, an album has many songs, etc.

Edit 1: As Victor Nicollet said, you want composition.

Edit 2: As stated in the comments, you actually want aggregation. The provided article explains the difference between the two.


If you let us know what you are wishing to accomplish, that may help us arrive at the correct answer. However, these relationships would normally be stored in your database.

0

精彩评论

暂无评论...
验证码 换一张
取 消