Search code examples
umlclass-diagram

class diagram - link is valid or not?


Class diagram

The above shows a class diagram. There is doThis(obj: Class B) in Class A which takes an object of ClassB and will eventually call obj.operation1().

Should there be a link between Class A and Class B only for this reason? Or there is no need to link them and Class A can call operation1() using object of Class B?


Solution

  • The links between in a class diagram can be one of the following:

    1. Association, and its sub-types Aggregation and Composition.
    2. Generalization
    3. Realization
    4. Dependency

    All the above links are reflected in the code. I will give some examples in java:

    Association If the association is mono-directional (the link has an arrow that points to B), then the class A has a field of class B.

    public class A {
      B b;
    }
    

    If the association is bidirectional (no arrows in the link) then also class B has a field of class A.

    public class B {
      A a;
    }
    

    Generalization

    In this case, A inherits B.

    public class A extends B {
       //class implementation
    

    Realization

    In this case B must be an interface, and A a class implementing it.

    public class A implements B {
       //class implementation
    

    Dependency

    In this case, class A depends on class B. What does this mean? It means that class B somehow exists in the code of class A. So, if classes A and B are in different packages, the class file of A has an import statement for class B

    import somepackage.B;
    
    public class A { 
       void someMethod() {
          B b = ...
       }
    }
    

    What about the specific diagram? The specific diagram does not specify navigability of the association, namely it is not stated which class keeps a reference to the other. Usually when there is no explicit specification of navigability, a bidirectional association is implied. The diagram with a bidirectional association between the classes translates to this:

    public class A {
      B b;
      public void doSomething(B obj) {
         obj.operation1();
      }
    }
    
    public class B {
      A a;
      public void operation1() {
         //some code in here...
      }
    } 
    

    If class A has no field of class B, but it only uses B as an argument in the doSomething(b) method, then there is no association, but rather a dependency. So you should replace the association link with a dependency link, and your diagram will be:

    dependency

    Should the dependency be omitted from the diagram?

    You don't need to present every link between two classes in a UML diagram. UML is a means of communication, so you can omit whatever does not add value to the message being communicated. In a more complicated scenario with more classes, most likely you don't want to present all dependencies or associations of a class, but only the ones that you think are important. Otherwise the diagram gets full of links and becomes unreadable.

    So if you think that the dependency of class A to class B is not something important, you can omit it from your diagram (since it is also presented in the signature of the doSomething(b) method. Usually you don't want to present each and every compile dependency in the diagram, but something more important.

    On the other hand if this relationship is part of the message that you want to communicate, then you should include it in the diagram. In this case, I find it very useful to decorate the dependency link with a meaningful stereotype, that describes the type of dependency. Something like <<invokes_operation1>> would be more descriptive, specially if operation1() is an important part of the functionality of class B.