software engineering i 02161
play

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


  1. Software Engineering I (02161) Class Diagrams Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2020

  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.” 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

  3. One system several models (abstractions)

  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

  5. Class Diagram ◮ Purpose: Communication and documentation ◮ Possible applications ◮ Knowledge modelling ◮ Domain modelling: model of the problem domain . . . Implementation: model of the solution domain

  6. 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() {} }

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

  8. 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

  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) { ... } }

  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; } }

  11. 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()); }

  12. 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; .... ... } }

  13. 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; ... .... } }

  14. 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; } }

  15. 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;} } }

  16. 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>(); ... }

  17. 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<>();

  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] lineItems OrderLine isPrepaid: Boolean[1] 1 *

  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] lineItems OrderLine isPrepaid: Boolean[1] 1 * lineItems: OrderLine[*]

  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); ...

  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

  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)

  23. Composite Aggregation ◮ The life of the part object is tied to the life of the containing object → A part can only be part of one object :Point {x = 0, y = 0 } :Point {x = 0, y = 10 } :Polygon :Point {x = 10, y = 10 } :Point {x = 10, y = 0 }

  24. Composite Aggregation ◮ The life of the part object is tied to the life of the containing object → A part can only be part of one object :Point :Circle {x = 0, y = 0 } :Point :Polygon {x = 0, y = 20 } :Point {x = 10, y = 10 }

  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; } }

  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 {abstract} Medium String signature String title String author Calendar borrowDate int fine() int maxBorrowInDays() boolean isOverdue() boolean isBorrowed() Book Cd int fine() int fine() int maxBorrowInDays() int maxBorrowInDays()

  27. Interfaces and abstract classes Martin Fowler: UML Distilled

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend