Mini project (support)
Implementing UML class diagrams in Java
Dr Artur Boronat
Acknowledgement: Luca Campese (CO2006 15-16)
CO2006
Introduction
Associations
Implementation
Classes
Generalization
Association
• Multiplicities • Properties
Introduction
Associations
Generalization
• Domain model:
• Implementation:
public class A { public A () {}
}
public class B extends A {
public B() {} }
Introduction Associations
Interface Implementation
• Domain model:
• Implementation:
public interface A { }
public class B implements A {
public B() {} }
Introduction Associations
Unidirectional one-to-one association: lower bound 0
• Domain model:
• Implementation:
public class A { private B b; public A() { } public B getB() {
return b; }
public void setB(B b) { this.b=b;
} }
Introduction Associations
Unidirectional one-to-one association: lower bound 1
• Domain model:
• Implementation 1: object A has enough information to instantiate B
public class A { private B b; public A() {
b = new B(); }
public B getB() { return b;
}
public void setB(B b) {
if (b != null) this.b=b;
} }
Introduction Associations
Unidirectional one-to-one association: lower bound 1 (II)
• Domain model:
• Implementation 2: otherwise
public class A {
private B b;
public A(B b) throws Exception {
if (b == null)
throw new Exception ();
this.b=b; }
public B getB() { return b;
}
public void setB(B b) {
if (b != null) this.b=b;
} }
Introduction Associations
Bidirectional one-to-one association I
• Domain model:
• Implementation:
public class A { private B b; public A() { } public B getB() {
return b; }
public void setB(B b) { if (this.b != b) {
this.b=b;
b.setA(this); }
} }
public class B {
private A a;
public B(A a) throws Exception {
if (a == null)
throw new Exception ();
this.a=a; }
public A getA() { return a;
}
public void setA(A a) {
if ((a != null) && (this.a != a)) {
this.a=a;
a.setB(this); }}}
Introduction Associations
Bidirectional one-to-one association II
• Domain model:
• Implementation: we need to make a choice as to which object creates the object at the other end of the link unless an object is given to the constructor
public class A { private B b; public A() {
b=new B(this); }
public B getB() { return b;
}
public void setB(B b) {
public class B {
private A a;
public B(A a) throws Exception {
if (a == null) throw new Exception();
this.a=a; }
public A getA() { return a;
}
public void setA(A a) {
if ((b != null) &&
(this.b != b)){
this.b=b;
b.setA(this); this.a=a;
}}} a.setB(this); }}}
if ((a != null) && (this.a != a)) {
Introduction
Associations
Unidirectional one-to-many association
• Domain model:
• Implementation:
public class A { private Set b; public A() {
b=new HashSet (); }
public void addB(B b) throws Exception { if (b == null)
throw new Exception (); this.b.add(b);
}
public void removeB(B b) {
this.b.remove(b); }
}
public class B { public B() {}
}
Introduction Associations
Unidirectional one-to-many association
• Domain model:
• Implementation: if an object A has enough information to instantiate class B
public class A { private Set b; public A() {
b=new HashSet ();
b.add(new B()); }
public void addB(B b) { this.b.add(b);
}
public void removeB(B b) {
if (this.b.size()>1) this.b.remove(b);
} }
public class B { public B() {}
}
Introduction
Associations
Unidirectional one-to-many association
• Domain model:
• Implementation: otherwise
public class A { private Set b; public A(B b) {
this.b=new HashSet ();
this.b.add(b); }
public void addB(B b) throws Exception { if (b == null)
throw new Exception (); this.b.add(b);
}
public void removeB(B b) {
if (this.b.size()>1) this.b.remove(b);
} }
public class B { public B() {}
}