Collaboration Diagram:
Given: class A has a private variable b of class B.
x() triggers 1, which triggers 1.1, which triggers 1.1.1 and 1.1.2
public class A {
private B b = new B();
public void x() {
y(); // 1: is applied to self
}
private void y() {
z(); // 1.1: is applied to self
}
private void z() {
b.q(this); // 1.1.1: is applied to b:B
r(); // 1.1.2: is applied to self
}
private void r() {
}
public void s() {
}
}
class B {
public void q(A a) {
a.s(); // 1.1.1.1: is applied backwards to a:A
}
}
UML Examples [13]
Collaboration Diagram:
class A {
public void x() {
B b = new B(); // 1:
C c = new C(); // 2:
D d = new D(); // 3:
b.y(this,c,d); // 4:
c.z(this,d); // 5:
q(); // 6:
}
public void q() {}
}
class B {
public void y(A a, C c, D d) {
d.r(); // 4.1:
c.z(a,d); // 4.2:
}
}
class C {
public void z(A a, D d) {
d.r(); // 4.2.1: and 5.1:
a.q(); // 4.2.2: and 5.2:
}
}
class D {
public void r() {}
}
UML Examples [14]
Static Structure Diagram:
Collaboration Diagram:
What is the Java code?
UML Examples [15]
Sequence Diagram:
class A {
private B b = new B();
private C c = new C();
public x() {
b.y(c); // 1: is applied to b, and passes along c
b.r(); // 2:
c.s(); // 3:
t(); // 4: is applied to self
}
private t() {
}
}
class B {
public y(C c) {
c.z(this); // 1.1:
}
public q() {
}
}
class C {
public z(B b) {
b.q(); // 1.1.1:
}
}
What is the Collaboration Diagram?
UML Example: Geometric Objects [16]
Static Structure Diagram (Inheritance):
Application illustrates inheritance, visibility, class vs. instance attributes/methods
Geometric Objects: Java [17]
import java.awt.Color; // package
public class Geo {
public static void main(String args[]) { // entry point
Circle a = new Circle("a",10); // object instance of class
Circle b = new Circle("b",20); // another object
Color color;
// access class variable
System.out.println("Number of circles: "+Circle.num_circles);
// access instance method
System.out.println("Circ of a: "+a.circumference()+" Area of b: "+b.area());
// access public instance variable
System.out.println("Radius of a: "+a.r);
// access instance method of grandparent
System.out.println("Location of a: "+a.getx()+","+a.gety());
// access instance method
System.out.println("Biggest area: "+a.bigger(b).area());
// access class method
System.out.println("Biggest area: "+Circle.bigger(a,b).area());
// access instance method of grandparent
System.out.println("Circle with biggest area: "+a.bigger(b).getName());
color = a.getColor();
System.out.println("Color of circle a: "+color.toString());
a.chgAppearance(); // make it darker
color = a.getColor();
System.out.println("Color of circle a: "+color.toString());
}
}
abstract class Primitive { // abstract=>cannot make an object instance
public static final int X=100; // class constant
public static final int Y=100;
private int x; // instance variable
private int y;
private String name; // String is class
protected Color color; // let subclasses have access
public Primitive(String name, int x, int y, Color color) { // constructor
this.name = name; // refer to self
this.x = x;
this.y = y;
this.color = color;
}
public void setx(int x) {this.x = x; } // instance method
public void sety(int y) { this.y = y; }
public int getx() { return x; }
public int gety() { return y; }
public String getName() { return name; }
public Color getColor() { return color; }
public void chgAppearance() { color = color.brighter(); }
}
Geometric Objects: Java [18]
class Conic extends Primitive { // inheritance
private int a; // ax^2 + bxy + cy^2 + dx + ey + f = 0
private int b;
private int c;
private int d;
private int e;
private int f;
public Conic(String name, int x, int y, Color color,
int a, int b, int c, int d, int e, int f) {
super(name,x,y,color); // call constructor of parent
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
}
class Circle extends Conic { // inheritance
public static final double PI=3.14159; // class constant
public static int num_circles=0; // class variable
public int r; // instance variable
public Circle(String name, int x, int y, Color color, int r) {
super(name,x,y,color,1,0,1,0,0,-r*r); // call constructor of parent
num_circles++; // count number of objects
this.r = r;
}
public Circle(String name, int r) { // multiple constructors
this(name,Primitive.X,Primitive.Y,Color.red,r);
}
public Circle(int r) { this("",r); }
public double circumference() { return 2*PI*r; }
public double area() { return PI*r*r; }
public Circle bigger(Circle c) { // instance method
if (c.r > this.r) return c; else return this; // return myself
}
public static Circle bigger(Circle a, Circle b) { // class method
if (a.r > b.r) return a; else return b;
}
public void chgAppearance() { // overriding method
color = color.darker(); // access protected variable
}
}