Software Engineering I (02161) Class Diagrams Assoc. Prof. Hubert - - PowerPoint PPT Presentation

software engineering i 02161
SMART_READER_LITE
LIVE PREVIEW

Software Engineering I (02161) Class Diagrams Assoc. Prof. Hubert - - PowerPoint PPT Presentation

Software Engineering I (02161) Class Diagrams Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2020 Model (definition) http://en.wiktionary.org/wiki/model . . . A simplified representation used to explain the


slide-1
SLIDE 1

Software Engineering I (02161)

Class Diagrams

  • Assoc. Prof. Hubert Baumeister

DTU Compute Technical University of Denmark

Spring 2020

slide-2
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.” 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-3
SLIDE 3

One system several models (abstractions)

slide-4
SLIDE 4

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-5
SLIDE 5

Class Diagram

◮ Purpose: Communication and documentation ◮ Possible applications

◮ Knowledge modelling ◮ Domain modelling: model of the problem domain . . . Implementation: model of the solution domain

slide-6
SLIDE 6

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-7
SLIDE 7

Communication

{abstract} Component cost() : double Part cost() : double CatalogueEntry cost : double name : String number : long Assembly add(Component) cost() : double components * entry 1

slide-8
SLIDE 8

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-9
SLIDE 9

Correspondence between Classes and Programs

«Stereotype» PackageName::ClassName {Some Properties} +name1 : String = "abc" name2 : OtherClass[*]

  • name3 : int {read only}

#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-10
SLIDE 10

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-11
SLIDE 11

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)

  • birthyear; }

}

Person birthyear : int /age : int { result = currentYear - birthyear }

slide-12
SLIDE 12

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-13
SLIDE 13

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-14
SLIDE 14

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-15
SLIDE 15

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-16
SLIDE 16

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-17
SLIDE 17

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-18
SLIDE 18

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-19
SLIDE 19

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-20
SLIDE 20

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-21
SLIDE 21

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-22
SLIDE 22

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-23
SLIDE 23

Composite Aggregation

◮ The life of the part object is tied to the life of the containing

  • bject

→ 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-24
SLIDE 24

Composite Aggregation

◮ The life of the part object is tied to the life of the containing

  • bject

→ 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-25
SLIDE 25

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-26
SLIDE 26

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-27
SLIDE 27

Interfaces and abstract classes

Martin Fowler: UML Distilled

slide-28
SLIDE 28

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-29
SLIDE 29

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-30
SLIDE 30

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»