Search code examples
oopclass-diagram

Class Diagram OOP


If two classes are associated regularly, do they need to contain each other's objects as attributes? For example, a Post class and a Comment class on a social media platform. Should the Post class contain an array of comment objects as an attribute? And should the Comment class contain a post object as an attribute?


Solution

  • This is a common mistake of redundant information. It does not need to be shown in the class diagram as a field because it is derived from the association.
    This is a class diagram that covers what you have mentioned in its simplest form. enter image description here

    As you can see, objects for each class do not need to be added as fields.