Software Engineering I (02161) Week 7: Sequence Diagrams, Class - - PowerPoint PPT Presentation

software engineering i 02161
SMART_READER_LITE
LIVE PREVIEW

Software Engineering I (02161) Week 7: Sequence Diagrams, Class - - PowerPoint PPT Presentation

Software Engineering I (02161) Week 7: Sequence Diagrams, Class Diagrams II, Layered Architecture Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2017 Contents Recap Sequence Diagrams Object-orientation:


slide-1
SLIDE 1

Software Engineering I (02161)

Week 7: Sequence Diagrams, Class Diagrams II, Layered Architecture

  • Assoc. Prof. Hubert Baumeister

DTU Compute Technical University of Denmark

Spring 2017

slide-2
SLIDE 2

Contents

Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

slide-3
SLIDE 3

Recap

◮ How to get from requirements to the design?

◮ Execute the use case scenarios / user stories ◮ Different techniques: ”in your head”, Class Responsibility

Collaboration (CRC) cards, Class diagrams + Sequence diagrams

◮ Communicating your design:

◮ Class diagrams

slide-4
SLIDE 4

Contents

Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

slide-5
SLIDE 5

Sequence Diagram: Computing the price of an order

◮ Class diagram

Order calculate price calculate base price calculate discounts Product name price Customer name discount info OrderLine quantity * 1 1

◮ Problem:

◮ What are the operations doing?

slide-6
SLIDE 6

Sequence diagram

slide-7
SLIDE 7

Synchronous– vs Asynchronous Calls:

Synchronous

b.m(4); c.n(...) // Starts after m has returned

Asynchronous

// (new Thread(){ public void run() {b.m(4);}}).start(); new Thread(() -> {b.m(4);}).start(); // Using Lambdas from Java 8 c.n(...) // Starts immediately after m has been called

return sync call async call b:B a:A

slide-8
SLIDE 8

Interaction Frames Example

Realising an algorithm using a sequence diagram public void dispatch() { for (LineItem lineItem : lineItems) { if (lineItem.getValue() > 10000) { careful.dispatch(); } else { regular.dispatch(); } } if (needsConfirmation()) { messenger.confirm(); } }

slide-9
SLIDE 9

Realisation with Interaction Frames

slide-10
SLIDE 10

Interaction Frame Operators I

slide-11
SLIDE 11

Usages of sequence diagrams

◮ Show the exchange of messages of a system

◮ i.e. show the execution of the system ◮ in general only, one scenario ◮ with the help of interaction frames also several scenarios

◮ For example use sequence diagrams for

◮ Designing (c.f. CRC cards) ◮ Visualizing program behaviour

slide-12
SLIDE 12

Contents

Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

slide-13
SLIDE 13

Centralized control

Order calculate price calculate base price calculate discounts Product name price Customer name discount info OrderLine quantity * 1 1

slide-14
SLIDE 14

Centralised control

slide-15
SLIDE 15

Distributed control

slide-16
SLIDE 16

Distributed Control: Class diagram

Order calculate price calculate base price calculate discounts Product name price get price for quantity Customer name discount info calculate discount OrderLine quantity calculate price * 1 1

slide-17
SLIDE 17

Centralized vs Distributed control

◮ Centralized control

◮ One method ◮ Data objects

→ procedural programming language

◮ Distributed control

◮ Objects collaborate ◮ Objects = data and behaviour

→ Object-orientation

◮ Advantage

◮ Easy to adapt

→ Design for change

slide-18
SLIDE 18

Design for Change

How to add a new type of product, which is cheaper in large quantities?

slide-19
SLIDE 19

Design for Change

How to add a new type of product, which is cheaper in large quantities?

sd: Order calculateOrder calculateOrder calculatePrice getPrice(n) calculatePrice getPrice(n) getDiscountValue(o) getBaseValue User Order OrderLine Product BulkProduct Customer

slide-20
SLIDE 20

Contents

Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Implementing Associations Bi-directional associations Associations Layered Architecture

slide-21
SLIDE 21

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

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

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

Encapsulation problem: getStudents

Student University * University dtu = new University("DTU"); .. Set<Student> students = dtu.getStudents();

slide-26
SLIDE 26

Encapsulation problem: getStudents

Student University * University dtu = new University("DTU"); .. Set<Student> students = dtu.getStudents(); Student hans = new Student("Hans"); students.add(hans); Student ole = dtu.findStudentNamed("Ole"); students.remove(ole); ...

Solution: getStudents returns an unmodifiable set

public void Set<Student> getStudents() { return Collections.unmodifiableSet(students); }

slide-27
SLIDE 27

Encapsulation problem: setStudents

Student University * University dtu = new University("DTU"); .. Set<Student> students = new HashSet<Student>(); dtu.setStudents(students);

slide-28
SLIDE 28

Encapsulation problem: setStudents

Student University * University dtu = new University("DTU"); .. Set<Student> students = new HashSet<Student>(); dtu.setStudents(students); Student hans = new Student("Hans"); students.add(hans); Student ole = dtu.findStudentNamed("Ole"); students.remove(ole); ...

Solution: setStudents copies the set

public void setStudents(Set<Student> stds) { students = new HashSet<Student>(stds); }

slide-29
SLIDE 29

Solution: How to change the association?

Student University * public class University { private Set<Student> bs = new HashSet<Student>(); public void addStudent(Student s) {students.add(student);} public void containsStudent(Student s) {return students.contains(s);} public void removeStudent(Student s) {students.remove(s);} }

Even better: domain specific methods like registerStudent

slide-30
SLIDE 30

Bi-directional associations

0..1 arbejdsgiver * ansatte Firma navn: String {read only} Person navn: String {read only}

Implemented as two uni-directional associations

0..1 arbejdsgiver Firma navn: String {read only} Person navn: String {read only} * ansatte

→ Problem of referential integrity

slide-31
SLIDE 31

Referential Integrity

f1:Firma p1:Person p2:Person f2:Firma

slide-32
SLIDE 32

Referential Integrity: setArbejdsgiver

f1:Firma p1:Person p2:Person f2:Firma

slide-33
SLIDE 33

Referential Integrity: addAnsatte

f1:Firma p1:Person p2:Person f2:Firma

slide-34
SLIDE 34

Library application

LibraryApp ... Calendar getDate() registerUser(..) addMedium(..) ... Book User DateServer Calendar getDate() 0..1 * 0..1 * 1 * borrowedBooks 0..1 Address 1 *

slide-35
SLIDE 35

Contents

Recap Sequence Diagrams Object-orientation: Centralized vs Decentralized Control/Computation Implementing Associations Layered Architecture

slide-36
SLIDE 36

Low Coupling

High coupling

A B D E C F

slide-37
SLIDE 37

Low Coupling

High coupling

A B D E C F

Low coupling

A B D E C F

slide-38
SLIDE 38

High Cohesion

Low Cohesion

Person name cpr-number companyName work-address-street work-address-city home-address-street home-address-city

slide-39
SLIDE 39

High Cohesion

Low Cohesion

Person name cpr-number companyName work-address-street work-address-city home-address-street home-address-city

High Cohesion

Address street city Company name Person name cpr-number works at home address address

slide-40
SLIDE 40

Layered Architecture

Eric Evans, Domain Driven Design, Addison-Wesley, 2004

slide-41
SLIDE 41

Example Vending Machine

Two different presentation layers; same application layer

◮ Swing GUI ◮ Command line interface

Current Money: DKK 5 0) Exit 1) Input 1 DKK 2) Input 2 DKK 3) Input 5 DKK 4) Select banana 5) Select apple 6) Cancel Select a number (0-6): Rest: DKK 2 Current Money: DKK 0 Dispensing: Apple

slide-42
SLIDE 42

Application Layer

«enumeration» Fruit APPLE BANANA VendingMachine dispensedItem: Fruit currentMoney: int totalMoney: int restMoney: int input(money: int) select(f: fruit) cancel() *

slide-43
SLIDE 43

Architecture

Presentation Layer VendingMachine dispensedItem: Fruit currentMoney: int totalMoney: int restMoney: int input(money: int) select(f: fruit) cancel() * VendingMachineUI «enumeration» Fruit APPLE BANANA Presentation Layer VendingMachineTextUI Application Layer

slide-44
SLIDE 44

Presentation Layer: Swing GUI

sd:buy apple

slide-45
SLIDE 45

Presentation Layer: Swing GUI

public class VendingMachineUI extends javax.swing.JFrame implements java.util.Observer { private VendingMachine vendingMachine = new VendingMachine(10, 10); ... private JButton fiveCrowns = new JButton(); private JTextField currentM = new JTextField(); ... private void initComponents() { fiveCrowns.setText("DKK 5"); fiveCrowns.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { vendingMachine.input(5);; } } ) ... }; ... public void update(Observable o, Object arg) { currentM.setText("" + vendingMachine.getCurrentMoney()); ... } }

slide-46
SLIDE 46

Presentation Layer: Command Line Interface

sd:buy apple

slide-47
SLIDE 47

Advantages of the separation

1 Presentation layer easily changed 2 Additional presentation layers can be added easily without having to reimplement the business logic

◮ mobile app in addition to desktop and Web application

3 Automatic tests: test the application and domain layer: test ”under the GUI”

◮ Exam project: You should model the application and

domain layer with your class diagrams and sequence diagrams

slide-48
SLIDE 48

Next Week

◮ State machines ◮ Example of a GUI and persistency layer for the library

application

◮ Version control