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 workings of a real world system or event. ◮ ”The computer weather model did not correctly predict the path of the hurricane.”
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
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.” Clay model of a car A plane in a windtunnel
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.” Clay model of a car A plane in a windtunnel → Abstraction ◮ Focus on some aspects only, e.g. air resistance ◮ Disregard the other aspects ◮ Aggregate details by introducing new concepts
One system several models (abstractions)
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
Class Diagram ◮ Purpose: Communication and documentation ◮ Possible applications ◮ Knowledge modelling ◮ Domain modelling: model of the problem domain . . . Implementation: model of the solution domain
Communication public class Assembly extends Component { public double cost() { } public void add(Component c) {} private Collection<Component> public abstract class Component { components; public abstract double cost(); } } public class CatalogueEntry { public class Part extends Component private String name = ""; private CatalogueEntry entry; public String getName() {} public CatalogueEntry getEntry() {} private long number; public double cost(){} public long getNumber() {} public Part(CatalogueEntry entry){} private double cost; public double getCost() {} }
Communication {abstract} Component components * cost() : double CatalogueEntry Part Assembly cost : double entry cost() : double add(Component) name : String cost() : double 1 number : long
Components of a class diagram {abstract} Component components * cost() : double CatalogueEntry Part Assembly cost : double cost() : double add(Component) entry name : String cost() : double 1 number : long ◮ Associations ◮ Classes ◮ Role names ◮ Attributes ◮ Multiplicities ◮ Methods ◮ Generalization
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) { ... } }
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; } }
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()); }
Java: Private attributes and getter and setter Person public class Person { age : int {read only} private int birthyear; private int age; public int getAge() { return Calendar.getInstance() .get(Calendar.YEAR) - birthyear; } } public class Person { private int age; public int getAge() Person { return age; } birthyear : int } /age : int { result = currentYear - birthyear } for (Person p : persons) { System.out.println("age = ",p.getAge()); }
Associations between classes works for Company Person employee 0..1 * ◮ Company has a field employee s ◮ Person has field company public class Company public class Person { { private Set<Person> employees; private Company company; .... ... } }
Associations between classes: navigability works for Company Person employee 0..1 * ◮ Company has a field employee s ◮ Person does not have field company public class Company public class Person { { private Set<Person> employees; ... .... } }
Implementing Associations: Cardinality 0..1 A B 0..1 Associations and attributes are treated the same A b: B ◮ Field can be null public class A { private B b; public B getB() { return b; } public void setB(B b) { this.b = b; } }
Implementing Associations: Cardinality 1 A B 1 ◮ Field may not be null
Implementing Associations: Cardinality 1 A B 1 ◮ 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;} } }
Implementing Associations: Cardinality * A B * Default: Unordered, no duplicates public class A { private Set<B> bs = new HashSet<B>(); ... }
Implementing Associations: Cardinality * A B * Default: Unordered, no duplicates public class A { private Set<B> bs = new HashSet<B>(); ... } A {ordered} B * public class A { private List<B> bs = new ArrayList<B>(); ... }
Interface Collection < E > Operation Description boolean add(E e) returns false if e is in the collection returns true if e is in the collection boolean remove(E e) returns true if e is in the collection boolean contains(E e) allows to iterate over the collection Iterator < E > iterator() int size() number of elements
Interface Collection < E > Operation Description boolean add(E e) returns false if e is in the collection returns true if e is in the collection boolean remove(E e) returns true if e is in the collection boolean contains(E e) allows to iterate over the collection Iterator < E > iterator() 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<>();
Attributes and Associations: are interchangable public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
Attributes and Associations: are interchangable public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
Attributes and Associations: are interchangable public class Order { private Date date; private boolean isPrepaid = false; private List<OrderLine> lineItems = new ArrayList<OrderLine)(); ... }
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] lineItems OrderLine isPrepaid: Boolean[1] 1 *
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] lineItems OrderLine isPrepaid: Boolean[1] 1 * lineItems: OrderLine[*]
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); ...
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
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)
Recommend
More recommend