SLIDE 1 Software Engineering I (02161)
Class Diagrams
- Assoc. Prof. Hubert Baumeister
DTU Compute Technical University of Denmark
Spring 2020
SLIDE 2
Model (definition)
http://en.wiktionary.org/wiki/model . . . A simplified representation used to explain the workings of a real world system or event.
◮ ”The computer weather model did not correctly predict the path of the hurricane.”
SLIDE 3
Model (definition)
http://en.wiktionary.org/wiki/model . . . A simplified representation used to explain the workings of a real world system or event.
◮ ”The computer weather model did not correctly predict the path of the hurricane.” A plane in a windtunnel
SLIDE 4
Model (definition)
http://en.wiktionary.org/wiki/model . . . A simplified representation used to explain the workings of a real world system or event.
◮ ”The computer weather model did not correctly predict the path of the hurricane.” A plane in a windtunnel Clay model of a car
SLIDE 5
Model (definition)
http://en.wiktionary.org/wiki/model . . . A simplified representation used to explain the workings of a real world system or event.
◮ ”The computer weather model did not correctly predict the path of the hurricane.” A plane in a windtunnel Clay model of a car
→ Abstraction
◮ Focus on some aspects only, e.g. air resistance ◮ Disregard the other aspects ◮ Aggregate details by introducing new concepts
SLIDE 6
One system several models (abstractions)
SLIDE 7 UML
◮ Unified Modelling Language (UML) ◮ Set of graphical notations: class diagrams, state machines, sequence diagrams, activity diagrams, . . . ◮ Developed in the 90’s ◮ ISO standard
Wikipedia
SLIDE 8
Class Diagram
◮ Purpose: Communication and documentation ◮ Possible applications
◮ Knowledge modelling ◮ Domain modelling: model of the problem domain . . . Implementation: model of the solution domain
SLIDE 9
Communication
public class Assembly extends Component { public double cost() { } public void add(Component c) {} private Collection<Component> components; } public class CatalogueEntry { private String name = ""; public String getName() {} private long number; public long getNumber() {} private double cost; public double getCost() {} } public abstract class Component { public abstract double cost(); } public class Part extends Component private CatalogueEntry entry; public CatalogueEntry getEntry() {} public double cost(){} public Part(CatalogueEntry entry){}
SLIDE 10 Communication
{abstract} Component cost() : double Part cost() : double CatalogueEntry cost : double name : String number : long Assembly add(Component) cost() : double components * entry 1
SLIDE 11 Components of a class diagram
{abstract} Component cost() : double Part cost() : double CatalogueEntry cost : double name : String number : long Assembly add(Component) cost() : double components * entry 1
◮ Classes ◮ Attributes ◮ Methods ◮ Associations ◮ Role names ◮ Multiplicities ◮ Generalization
SLIDE 12 Correspondence between Classes and Programs
«Stereotype» PackageName::ClassName {Some Properties} +name1 : String = "abc" name2 : OtherClass[*]
#name4 : boolean
- f1(a1:int, a2:String[]) : float
+f2(x1:String,x2:boolean) : float f4(a:double) #f3(a:double) : String
package packagename; public class ClassName { public String name1 = "abc"; public List<OtherClass> name2 = new ArrayList<OtherClass>(); private int name3; protected static boolean name4; private static float f1(int a1, String[] a2) { ... } public void f2(String x1, boolean x2) { ... } abstract public void f4(a:double); protected String f3(double a) { ... } }
SLIDE 13
Public Attributes
Java convention for implementing public attributes ◮ attributes are private fields ◮ public getter and setter methods public class C { private int a; public int getA() { return a; } public void setA(int a) { this.a = a; } }
SLIDE 14
Java: Private attributes and getter and setter
Person age : int {read only}
public class Person { private int age; public int getAge() { return age; } } for (Person p : persons) { System.out.println("age = ",p.getAge()); }
SLIDE 15 Java: Private attributes and getter and setter
Person age : int {read only}
public class Person { private int age; public int getAge() { return age; } } for (Person p : persons) { System.out.println("age = ",p.getAge()); } public class Person { private int birthyear; private int age; public int getAge() { return Calendar.getInstance() .get(Calendar.YEAR)
}
Person birthyear : int /age : int { result = currentYear - birthyear }
SLIDE 16 Associations between classes
Company Person * employee 0..1 works for
◮ Company has a field employees ◮ Person has field company
public class Company { private Set<Person> employees; .... } public class Person { private Company company; ... }
SLIDE 17 Associations between classes: navigability
Company Person * employee 0..1 works for
◮ Company has a field employees ◮ Person does not have field company
public class Company { private Set<Person> employees; .... } public class Person { ... }
SLIDE 18
Implementing Associations: Cardinality 0..1
0..1 B A A b: B Associations and attributes are treated the same
◮ Field can be null
public class A { private B b; public B getB() { return b; } public void setB(B b) { this.b = b; } }
SLIDE 19
Implementing Associations: Cardinality 1
1 B A
◮ Field may not be null
SLIDE 20
Implementing Associations: Cardinality 1
1 B A
◮ Field may not be null
public class A { private B b = new B(); // 1st way of doing it public A(B b) { this.b = b;} // 2nd way public B getB() { // 3rd way if (b == null) {b = computeB();} return b; } public void setB(B b) { if (b != null) {this.b = b;} } }
SLIDE 21 Implementing Associations: Cardinality *
* B A
Default: Unordered, no duplicates
public class A { private Set<B> bs = new HashSet<B>(); ... }
SLIDE 22 Implementing Associations: Cardinality *
* B A
Default: Unordered, no duplicates
public class A { private Set<B> bs = new HashSet<B>(); ... }
* {ordered} B A
public class A { private List<B> bs = new ArrayList<B>(); ... }
SLIDE 23
Interface Collection<E>
Operation Description boolean add(E e) returns false if e is in the collection boolean remove(E e) returns true if e is in the collection boolean contains(E e) returns true if e is in the collection Iterator<E> iterator() allows to iterate over the collection int size() number of elements
SLIDE 24
Interface Collection<E>
Operation Description boolean add(E e) returns false if e is in the collection boolean remove(E e) returns true if e is in the collection boolean contains(E e) returns true if e is in the collection Iterator<E> iterator() allows to iterate over the collection int size() number of elements
Encapsulation: abstract away from the implementation
List<Person> employees = new ArrayList<>(); // not synchroniszed List<Person> employees = new Vector<>(); // syncrhonized List<Person> employees = new LinkedList<>();
SLIDE 25
Attributes and Associations: are interchangable
public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
SLIDE 26
Attributes and Associations: are interchangable
public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
SLIDE 27
Attributes and Associations: are interchangable
public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
SLIDE 28 Attributes and Associations: are interchangable
public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
◮ Use the type that shows the purpose of the diagram best
Order dateReceived: Date[0..1] isPrepaid: Boolean[1] OrderLine * 1 lineItems
SLIDE 29 Attributes and Associations: are interchangable
public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
◮ Wrong!
Order dateReceived: Date[0..1] isPrepaid: Boolean[1] lineItems: OrderLine[*] OrderLine * 1 lineItems
SLIDE 30
Qualified Associations
public class Order { private Map<Product,OrderLine> lineItems = new HashMap<Product,OrderLine>(); }
Basic operations on Map<K,V>: ◮ put(key,value) ◮ get(key)
Map<Product,OrderLine> lineItems = new HashMap<Product,OrderLine>(); ... lineItems.put(product,orderLine); OrderLine ol = lineItems.get(product); ...
SLIDE 31
Part of relationship
Special type of associations ◮ aggregation ◮ composition ◮ Use part of instead of has a
→ A car has an engine = an engine is part of the car → But Peter has a house = the house is part of Peter
SLIDE 32
Aggregation
◮ General ”part of” relationship ◮ Notation: empty diamond ◮ From the UML specification
◮ ”Precise semantics of shared aggregation varies by application area and modeller.” (from the UML 2.0 standard)
SLIDE 33 Composite Aggregation
◮ The life of the part object is tied to the life of the containing
→ A part can only be part of one object
:Polygon :Point {x = 10, y = 0 } :Point {x = 10, y = 10 } :Point {x = 0, y = 10 } :Point {x = 0, y = 0 }
SLIDE 34 Composite Aggregation
◮ The life of the part object is tied to the life of the containing
→ A part can only be part of one object
:Point {x = 10, y = 10 } :Point {x = 0, y = 20 } :Point {x = 0, y = 0 } :Circle :Polygon
SLIDE 35
Composition Rule: No outside access to parts
◮ No getter and setter for the parts! ◮ Parts are created in the constructor
public class Circle() { private Point center; public Circle(int x, in y) { center = new Point(x,y); } }
SLIDE 36
Composition Rule: No outside access to parts
◮ No getter and setter for the parts! ◮ Parts are created in the constructor
public class Circle() { private Point center; public Circle(int x, in y) { center = new Point(x,y); } }
◮ Alternatively: create a copy first
public class Polygon() { private List<Point> points = new ArrayList<>(); ... public add(Point p) { points.add(new Point(p.getX(),p.getY()); } ... }
SLIDE 37
Composition Rule: No outside access to parts
◮ No getter and setter for the parts! ◮ Parts are created in the constructor
public class Circle() { private Point center; public Circle(int x, in y) { center = new Point(x,y); } }
◮ Alternatively: create a copy first
public class Polygon() { private List<Point> points = new ArrayList<>(); ... public add(Point p) { points.add(new Point(p.getX(),p.getY()); } ... }
◮ Detachment of a part is possible
public class Car() { private Wheel[] wheels = new Wheel[4]; public Car() { wheels[0] = new Wheel(); .. } public Wheel detachWheel(int n) { Wheel wheel = wheels[n]; wheels[n] = null; return wheel; } }
SLIDE 38 Generalization / Inheritance
◮ Programming languages like Java: Inheritance
abstract public class Medium { ... } public class Book extends Medium { ... } public class Cd extends Medium { ... }
◮ UML: Generalization / Specialization
Cd int fine() int maxBorrowInDays() {abstract} Medium String signature String title String author Calendar borrowDate int fine() int maxBorrowInDays() boolean isOverdue() boolean isBorrowed() Book int fine() int maxBorrowInDays()
SLIDE 39 Interfaces and abstract classes
Martin Fowler: UML Distilled
SLIDE 40
Packages: Structure the classes of an application
Directory structure Packages
dtu.library.app dut.library.domain dtu.library.dto dtu.library.gui dtu.library.persistence
Layered Architecture
SLIDE 41
Packages
◮ Helps struture your application
SLIDE 42
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
SLIDE 43
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
SLIDE 44
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
◮ List: Collection of objects
◮ in java.util: full qualifed class java.util.List
SLIDE 45
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
◮ List: Collection of objects
◮ in java.util: full qualifed class java.util.List
◮ References to classes from other packages
SLIDE 46
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
◮ List: Collection of objects
◮ in java.util: full qualifed class java.util.List
◮ References to classes from other packages
1 Fully qualified name: java.util.List<Book> books
SLIDE 47
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
◮ List: Collection of objects
◮ in java.util: full qualifed class java.util.List
◮ References to classes from other packages
1 Fully qualified name: java.util.List<Book> books 2 import java.util.List and List<Book> books or import java.util.*
SLIDE 48
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
◮ List: Collection of objects
◮ in java.util: full qualifed class java.util.List
◮ References to classes from other packages
1 Fully qualified name: java.util.List<Book> books 2 import java.util.List and List<Book> books or import java.util.*
◮ Package names: based on URLs
◮ org.mockito → http://mockito.org
SLIDE 49
Packages
◮ Helps struture your application ◮ Distinguish classes with the same name
◮ List widget for building GUIs
◮ in java.awt: fully qualified class java.awt.List
◮ List: Collection of objects
◮ in java.util: full qualifed class java.util.List
◮ References to classes from other packages
1 Fully qualified name: java.util.List<Book> books 2 import java.util.List and List<Book> books or import java.util.*
◮ Package names: based on URLs
◮ org.mockito → http://mockito.org
◮ Packages and sub-packages: Hierarchy of packages
SLIDE 50 Package Notation UML
Looking into packages
dtu.library app User Dvd Cd Book Medium Address EMailServer DateServer LibraryApp domain «import» * 0..1
SLIDE 51 Package Notation UML
Looking into packages
dtu.library app User Dvd Cd Book Medium Address EMailServer DateServer LibraryApp domain «import» * 0..1
Relationship between packages
persistence gui domain dtu.library app dto «uses» «uses» «uses» «uses» «uses»