Tutorial 4 : Composition

What is Composition Relationship?
  • it is also known as the "has a" relationship.
  • it is a directional relation which means the two classes (source class & target class) play different roles in the relation and affects one of the classes in the relationship but not the other
  • It models a whole-part relationship between objects, in this case, the Bed and Bedroom

    How is it implemented in code?
    directions on how to implement and coding above


    How does it look in UML Diagram?
    above is an example of how the Composition Relationship looks in UML
  • the two classes are connected by a solid black line followed by a diamond shape head
  • the diamond shape head points to the source class which is the Bedroom class
  • the other end is the target class which is the Bed class

    When should Composition be used?
    Composition Relationship is used when an object is needed to be there all the time. For example, in a bedroom, we always NEED a bed, or eles it wouldn't be considered a bedroom. Another example would be a lamp, without a lightbulb the lamp doesnt work, thus we always need a lightbulb in a lamp in order for it to work.




    Last Modified February 26, 2008 by Sang Quach.