Monday, 7 March 2011

UML, Aggregation and Java

One problem you often see when dealing with UML is that some developers don’t get the idea that what you see in class diagram should translate directly into code they draw a class diagram and write some code and neither bare any resemblance to each other. This, and other blog forthcoming blogs in the series, demonstrate the link between diagrams and code.




  public class A {
 
   
private B instanceVar;
   
   
/**
     * Closed diamond. Lifetime of aggregated variable is
     * determined by this class
     */
   
public void method() {
     
instanceVar = new B();
      System.out.println
(instanceVar);
   
}
  }
 
 
 
public class B {
   
  }

  public class A {

   
private final String instanceVar;

   
/**
     * Open diamond. Lifetime of aggregated variable does NOT depend on this class. Use the
     * open diamond when illustrating dependency injection.
     *
     *
@param instanceVar
     *            The aggregated variable.
     */
   
public A(String instanceVar) {
     
this.instanceVar = instanceVar;
   
}

   
/**
     * Do something... use the variable.
     */
   
public void method() {
     
System.out.println(instanceVar);
   
}
  }
 
 
public class B {
   
   
private String instanceVar;
   
   
/**
     *
@param arg0
     */
   
public B(String arg0) {
     
this.instanceVar = arg0;
   
}
  }

No comments: