educative.io

Why the comment class shouldn't have the identifier that whenther it is question or answer

public class Comment {
private int id;
private String content;
private int flagCount;
private int upvotes;
private Date creationDate;
private User postedBy;

private Post post; // Added this field

public Post getPost() {
return post;
}

public void setPost(Post post) {
this.post = post;
}
}


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Code for Stack Overflow

please help someone


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: Code for Stack Overflow

Hi @Sahil_Siddiqui !!
In the Stack Overflow problem, the Comment class doesn’t need an identifier to specify whether it is associated with a question or an answer explicitly. This design choice is aligned with the principles of object-oriented programming and data modeling.

Here’s why the Comment class doesn’t need a specific identifier for the associated post type (question or answer):

  1. Inheritance and Polymorphism: Typically, in an object-oriented design, Questions and Answers would be subclasses of a common superclass (e.g., Post or Content). Comments can be associated with any post, whether it’s a question or an answer. By utilizing inheritance and polymorphism, you can create a common structure for comments that can be used universally with both questions and answers.

  2. Flexibility and Extensibility: By not tying comments to a specific post type, you allow for greater flexibility and extensibility in your code. If you later decide to introduce new types of posts or content, such as blog posts or articles, you can reuse the Comment class without modification.

  3. Simpler Data Model: Having a separate identifier for the post type (question or answer) in the Comment class would introduce redundancy and complexity into the data model. You’d need to manage and synchronize this identifier with the associated post, which can lead to potential inconsistencies and maintenance challenges.

  4. Consistency with Object-Oriented Principles: Object-oriented design encourages modeling real-world entities and their relationships. In reality, comments are associated with content or posts in a more generic way, without being tightly coupled to specific post types.

To determine the associated post type (question or answer), you can navigate the object relationships. For example, you can provide methods within the Comment class to access the parent post, and then determine its type programmatically if needed.
I hope it helps. Happy Learning :blush:

2 Likes

could you please give example on this there is lots of confusion on this ::::::::::: “you can provide methods within the Comment class to access the parent post, and then determine its type programmatically if needed”


Course: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles
Lesson: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles/use-case-diagram-for-the-movie-ticket-booking-system

Sure, let’s clarify how you can provide methods within the Comment class to access the parent post (question or answer) and determine its type programmatically if needed. A simplified example in Java:

public class Comment {
    private int id;
    private String content;
    private int flagCount;
    private int upvotes;
    private Date creationDate;
    private User postedBy;

    private Post parentPost; // Reference to the parent post (question or answer)

    // Constructor and other Comment methods

    public void setParentPost(Post post) {
        this.parentPost = post;
    }

    public Post getParentPost() {
        return parentPost;
    }

    // Method to determine the type of the parent post (question or answer)
    public String getParentPostType() {
        if (parentPost instanceof Question) {
            return "Question";
        } else if (parentPost instanceof Answer) {
            return "Answer";
        } else {
            return "Unknown"; // Handle other post types if necessary
        }
    }

    // Other Comment methods
}

In this example:

  1. We have a parentPost field in the Comment class, which represents the parent post (question or answer) associated with the comment.

  2. The setParentPost method allows you to set the parent post for a comment.

  3. The getParentPost method allows you to retrieve the parent post associated with a comment.

  4. The getParentPostType method is used to programmatically determine the type of the parent post. It checks whether the parent post is an instance of Question or Answer (assuming Question and Answer are subclasses of a common Post class). If the parent post type is something else (e.g., a new post type introduced in the future), you can handle it accordingly.

Here’s how you can use these methods:

Comment comment = new Comment();
Question question = new Question();
Answer answer = new Answer();

comment.setParentPost(question); // Set the parent post as a question
System.out.println("Parent Post Type: " + comment.getParentPostType()); // Output: Parent Post Type: Question

comment.setParentPost(answer); // Set the parent post as an answer
System.out.println("Parent Post Type: " + comment.getParentPostType()); // Output: Parent Post Type: Answer

By providing these methods, you can access the parent post associated with a comment and determine its type (question or answer) programmatically without the need for an explicit identifier in the Comment class.
I hope it helps. Happy Learning :blush: