oopdesign-patternsumlclass-diagramclass-relationship

<<use>> or <<create>> relationship in program code


I can't figure out what a <<use>> or <<create>> relationship looks like in program code? Can someone give me an example?

Thanks


Solution

  • The «Create» dependency says that an object of one class creates instances of another one. A typical example is the factory pattern. The wikipedia article shows both the UML class diagram with create dependency and an example code.

    The «use» dependency tells that objects of one class uses instances of another class and therefore need to know about that other class. This other SO answer explains all the details. A typical example is when that some operations (methods) have parameters of the type of the other class:

     class B { ... }
     class A {
       public doSomething(B b) { ... }
     }
    

    enter image description here