-2

I'm creating a small social media website, and I'm faced with a dilemma. There are 2 ways I can design my classes and database that I currently know of.

  1. Users can make posts on the website by uploading images, videos, and plain text. My initial thought was to make a class for each post type and they inherit from a parent post class, I would then write all the post attributes of the post class to a database table called posts.

  2. Instead of the use of inheritance, I would create a table for each type of post (image, video, text), then write each type of user post to their own separate tables. Next I would create a 4th table that selects all the information from the other three tables and creates an object for each post. Then each post can be displayed on the website. This database would be in third normal form.

1

I prefer the second option in this scenario. It would result in more flexibility and ultimately more functionality for your social media website.

I am not sure for what exactly you are planning. But when I think of social media websites, I want to post text and attach videos and/or photos to that post. So it might make sense to allow both text and a photo to belong to a post. This is not possible and won't ever be possible if you go for the inheritance approach.

In more general terms you might also consider the composition over inheritance principle. Although object-oriented languages seem to be centered around inheritance and you often learn inheritance as one of the core feature of OOP, it is often advisably to prefer composing objects instead of inheriting from them. Your second approach is exactly that: you compose the post with another (or several other) objects, which are text/photos/videos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.