UML Class Diagram: Abstract or Interface?

I am modeling a class diagram and have spotted an opportunity to simplify it slightly. What I want to know is, would this it be better to implement an abstract class or an interface?

The scenario is this, I have the classes:

  • Artist
  • Genre
  • Album
  • Song

All of which share the methods getName, setName, and getCount (playcount that is). Would it be best to create an abstract ‘Music’ class with the aforementioned abstract methods, or should I create an interface, since the classes that implement the interface have to include all of the interface’s methods (I think, correct me if I’m wrong).

I hope I’ve given enough detail, please ask questions if I haven’t.

Thanks!

1

You should create an abstract class.

Generally speaking, a song can consist of a Title / Artist / Album / Genre. All of those are just a string of data which can be handled the same way.
By using an abstract class you don’t have to write mentioned “getName”, “setName” and “getCount” more than once, which sounds pretty neat.

Using an interface would just tell you that the functions should exist in each class, but you’d still have to write them separately. However, if you come up with methods they all have in common, but which work differently for each class, I suppose an interface would make more sense.

What bugs me though is that you want them to be classes. For example, since you would want an album to have it’s own “Times played”, you’d have to make sure that there’s only one instance of that specific album. Each time a new song is added, you’d either have to search through all existing classes with that name and set a pointer or something similar to the already existing one.

Then again, the good part about using an album as an instance of a class, is that it’s possible to have several albums with the same “name” without counting them as the same album. Err.. anyways, my mind has drifted away and this is not really related to the question.

1

I would use interfaces instead.
From my experience, there are 2 reasons for this:

  • Only use (abstract) class at the root of the diagram when you are so confident that sub-classes are very very similar
  • Unless you are very fluent in the area, will you need to make lot of modifications to not just your code but even your design too. This is when interfaces will be much more suitable, changing around the interface inheritance tree is much less disruptive than dealing with abstract classes.

Think about the Venn diagram and two not-equal sets of functionalities. A and B. In java you can only extend one abstract class, which one should you choose?
If you choose A, some time in the future, the need arises that you didn’t anticipate so B inflates to be bigger than A. You will need to change to B and then something that you didn’t anticipated arises and you change back to A.

Is it better to just use interfaces and then when the code base is stable, you refactor and common up the same functionalities in an abstract class.
Interfaces are much more agile and suitable for those constant changes. Of course if you already know way too well what will be anticipated, then by all means go with abstract classes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật