Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UML Class Diagram: Attribute or Association?

Now i have two classes, named patient and doctor:

Patient() {
     public:
        //functions here
    private:
    Doctor doctor;
    Date dateAdmitted;
    Date dateDischarged;
}

Doctor() {
    public:
    //functions here
    private:
    //data members here
}

In my UML class Diagram for patient class, do i need to include the doctor and date as attribute? or i just represent them by linking them as association?

If attribute it should be like:

Patient

doctor : Doctor

dateAdmitted : Date

dateDischarged : Date

like image 874
veeyikpong Avatar asked Apr 24 '14 06:04

veeyikpong


People also ask

What is attributes in UML class diagram?

In UML models, attributes represent the information, data, or properties that belong to instances of a classifier. A classifier can have any number of attributes or none at all. Attributes describe a value or a range of values that instances of the classifier can hold.

What is the difference between an attribute and an association?

James Rumbaugh has written, "Basically, attributes are for representing relationships between objects and values without identity, whereas associations are for representing relationships between objects and other objects" (Rumbaugh 1996).

What is association in UML class diagram?

In UML models, an association is a relationship between two classifiers, such as classes or use cases, that describes the reasons for the relationship and the rules that govern the relationship. In UML diagrams, an association class is a class that is part of an association relationship between two other classes.

Can UML represent class attributes?

UML provides mechanisms to represent class members, such as attributes and methods, and additional information about them like constructors.


1 Answers

According to UML syntactical rules, both solutions are valid - both class attributes and associated classes are so called class properties and can be shown as attributes (inside the class) or as separate classes, linked via association. For both these class features you can define name, multiplicity, scope, etc. Please refer to UML spec for detailed technical information.

However, the common practice if the following:

  • if the property is basic data type (int, boolean, date, etc) -> show it inside a class, as attribute
  • if the property is full fledged class -> show it as a separate class entity and use association to display their relationship

This practice make sense, as "int", "boolean" or "date" do not have their own custom properties and it is enough to show them inside a class (withoult losing information about them). Classes, on the other side, have their own features (attributes, methods and own associations, generalizations, etc) and therefore "deserve" more space on the diagram.

Concluding with the direct answer to your question: show Doctor as a separate class on the diagram, connected with Patient via association (note the property name displayed as associationEnd name). Keep both dates inside the Patient class:

enter image description here

The following diagram is equivalent and valid, but you might agree that the first one is visually clearer (imagine some atts, methods and relationships or the Doctor class) and therefore recommended:

enter image description here

UPDATE (after the comments)

enter image description here

Note: Composition is used for Dates, to reflect a strong relationship of the Whole-Part kind and the fact that these Dates cannot be unlinked from their context. The other association (Patient-Doctor) is a common assotiation and the corresponding link can be broken anytime (for example to change a Patient's Doctor).

like image 105
Aleks Avatar answered Sep 29 '22 20:09

Aleks