Showing posts with label Class Diagram. Show all posts
Showing posts with label Class Diagram. Show all posts

Wednesday, 7 August 2013

UML Class Diagram

There are five key relationships between classes in a UML class diagram : dependency, aggregation, composition, inheritance and realization. These five relationships are depicted in the following diagram:
  • Dependency : class A uses class B
  • Aggregation : class A has a class B
  • Composition : class A owns a class B
  • Inheritance : class B is a Class A  (or class A is extended by class B)
  • Realization : class B realizes Class A (or class A is realized by class B)

What I hope to show here is how these relationships would manifest themselves in Java so we can better understand what these relationships mean and how/when to use each one. The above relationships are read as follows:

Dependency :
class A uses class B
Is represented when a reference to one class is passed in as a method parameter to another class. For example, an instance of class B is passed in to a method of class A:
public class A {

public void doSomething(B b) {

}

}

Aggregation :
class A has a class B
If class A stored the reference to class B for later use we would have a different relationship called Aggregation. A more common and more obvious example of Aggregation would be via setter injection:
public class A {

private B _b;

public void setB(B b) {
_b = b;
}

}

Composition :
class A owns a class B
Aggregation is the weaker form of object containment (one object contains other objects). The stronger form is called Composition. In Composition the containing object is responsible for the creation and life cycle of the contained object. Following are a few examples of Composition. First, via member initialization:
public class A {

private B _b = new B();

}

Second, via constructor initialization:
public class A {

private B _b;

public A() {
_b = new B();
} // default constructor

}

Third, via lazy init:
public class A {

private B _b;

public B getB() {
if (null == _b) {
_b = new B();
}
return _b;
} // getB()

}

Inheritance :
class B is a Class A  (or class A is extended by class B)
is a fairly straightforward relationship to depict in Java:
public class A {

...

} // class A

public class B extends A {

....

} // class B

Realization :
class B realizes Class A (or class A is realized by class B)
is also straighforward in Java and deals with implementing an interface:
public interface A {

...

} // interface A

public class B implements A {

...

} // class B


For more information about Association, Aggregation,and Composition visit this link.


How to link Java Classes to UML Class Diagram in JDeveloper

1.       Create an Aplication
2.       Create a Java Project  with type Custom Project
3.       Create UML project with type UML Project
3.1.    To Make UML Class Diagram:
3.1.1.  Create a new “Java Class Diagram”
3.1.2. Drag and drop the UML element into this diagram
3.1.3. Use a suitable relationship between each element
3.2.    To Link the UML to the Classes bi-directionally  and generate the classes from diagram
3.2.1. Select the UML Project
3.2.2. Right Click Select “Project Properties”
3.2.3. Go to “Project Source Paths”
3.2.4. Click Remove button to delete the original “src“ folder
3.2.5. Click add button and go through the source folder of the JAVA Project, and save this path

4.       Now your Application had a link between diagram and Classes