Josh Bloch Charlie Garrod School of Computer Science 15-214 1 - - PowerPoint PPT Presentation

josh bloch charlie garrod
SMART_READER_LITE
LIVE PREVIEW

Josh Bloch Charlie Garrod School of Computer Science 15-214 1 - - PowerPoint PPT Presentation

Principles of So3ware Construc9on: Objects, Design, and Concurrency Designing (sub-) systems A formal design process, part 2 Josh Bloch Charlie Garrod School of Computer Science 15-214 1 Administrivia Midterm exam Thursday Review


slide-1
SLIDE 1

1

15-214

School of Computer Science

Principles of So3ware Construc9on: Objects, Design, and Concurrency Designing (sub-) systems A formal design process, part 2

Josh Bloch Charlie Garrod

slide-2
SLIDE 2

2

15-214

Administrivia

  • Midterm exam Thursday

– Review session Wednesday, September 28th, 7-9 pm, HH B103

  • Homework 4

– Three parts, part A due Oct 6th – Design review mee9ngs next week

  • Collabora9on policy…
slide-3
SLIDE 3

3

15-214

Key concepts from last Thursday…

slide-4
SLIDE 4

4

15-214

Sequence and communica9on diagrams

slide-5
SLIDE 5

5

15-214

Design principles

  • Low coupling
  • Low representa9onal gap
  • High cohesion
slide-6
SLIDE 6

6

15-214

Building a domain model for a library system

A public library typically stores a collec9on of books, movies, or other library items available to be borrowed by people living in a community. Each library member typically has a library account and a library card with the account’s ID number, which she can use to iden9fy herself to the library. A member’s library account records which items the member has borrowed and the due date for each borrowed item. Each type of item has a default rental period, which determines the item’s due date when the item is borrowed. If a member returns an item a3er the item’s due date, the member owes a late fee specific for that item, an amount of money recorded in the member’s library account.

slide-7
SLIDE 7

7

15-214

One domain model for the library system

slide-8
SLIDE 8

8

15-214

Notes on the library domain model

  • All concepts are accessible to a non-programmer
  • The UML is somewhat informal

– Rela9onships are o3en described with words

  • Real-world "is-a" rela9onships are appropriate for a domain model
  • Real-word abstrac9ons are appropriate for a domain model
  • Itera9on is important

– This example is a first dra3. Some terms (e.g. Item vs. LibraryItem, Account

  • vs. LibraryAccount) would likely be revised in a real design.
  • Aggregate types are usually modeled as classes
  • Primi9ve types (numbers, strings) are usually modeled as adributes
slide-9
SLIDE 9

9

15-214

Build a domain model for Monopoly

slide-10
SLIDE 10

10

15-214

Build a domain model for Monopoly

Monopoly is a game in which each player has a piece that moves around a game board, with the piece’s change in loca9on determined by rolling a pair of dice. The game board consists of a set of proper9es (ini9ally owned by a bank) that may be purchased by the players. When a piece lands on a property that is not owned, the player may use money to buy the property from the bank for that property’s price. If a player lands on a property she already owns, she may build houses and hotels on the property; each house and hotel costs some price specific for the property. When a player’s piece lands on a property owned by another player, the owner collects money (rent) from the player whose piece landed on the property; the rent depends on the number of houses and hotels built on the property. The game is played un9l only one remaining player has money and property, with all the other players being bankrupt.

slide-11
SLIDE 11

11

15-214

Ar9facts of this design process

  • Model / diagram the problem, define objects

– Domain model (a.k.a. conceptual model)

  • Define system behaviors

– System sequence diagram – System behavioral contracts

  • Assign object responsibili9es, define interac9ons

– Object interac9on diagrams

  • Model / diagram a poten9al solu9on

– Object model

Understanding the problem Defining a solu9on

slide-12
SLIDE 12

12

15-214

Ar9facts of this design process

  • Model / diagram the problem, define objects

– Domain model (a.k.a. conceptual model)

  • Define system behaviors

– System sequence diagram – System behavioral contracts

  • Assign object responsibili9es, define interac9ons

– Object interac9on diagrams

  • Model / diagram a poten9al solu9on

– Object model

Today

slide-13
SLIDE 13

13

15-214

Understanding system behavior with sequence diagrams

  • A system sequence diagram is a model that shows, for one

scenario of use, the sequence of events that occur on the system’s boundary

  • Design goal: Iden9fy and define the interface of the system

– Two components: A user and the overall system

slide-14
SLIDE 14

14

15-214

Understanding system behavior with sequence diagrams

  • A system sequence diagram is a model that shows, for one

scenario of use, the sequence of events that occur on the system’s boundary

  • Design goal: Iden9fy and define the interface of the system

– Two components: A user and the overall system

  • Input: Domain descrip9on and one use case
  • Output: A sequence diagram of system-level opera9ons

– Include only domain-level concepts and opera9ons

slide-15
SLIDE 15

15

15-214

One sequence diagram for the library system

Use case scenario: A library member should be able to use her library card to log in at a library system kiosk and borrow a book. A3er confirming that the member has no unpaid late fees, the library system should determine the book’s due date by adding its loan period to the current day, and record the book and its due date as a borrowed item in the member’s library account.

slide-16
SLIDE 16

16

15-214

One sequence diagram for the library system

Use case scenario: A library member should be able to use her library card to log in at a library system kiosk and borrow a book. A3er confirming that the member has no unpaid late fees, the library system should determine the book’s due date by adding its loan period to the current day, and record the book and its due date as a borrowed item in the member’s library account.

slide-17
SLIDE 17

17

15-214

Build one system sequence diagram for Monopoly

Use case scenario: When a player lands on an unowned property and has enough money to buy the property, she should be able to buy the property for the property’s price. The property should no longer be purchasable from the bank by other players, and money should be moved from the player to the bank.

slide-18
SLIDE 18

18

15-214

Formalize system behavior with behavioral contracts

  • A system behavioral contract describes the pre-condi9ons and

post-condi9ons for some opera9on iden9fied in the system sequence diagrams

– System-level textual specifica9ons, like so3ware specifica9ons

slide-19
SLIDE 19

19

15-214

A system behavioral contract for the library system

Opera9on: borrow(item) Pre-condi9ons: Library member has already logged in to the system. Item is not currently borrowed by another member. Post-condi9ons: Logged-in member's account records the newly-borrowed item, or the member is warned she has an outstanding late fee. The newly-borrowed item contains a future due date, computed as the item's rental period plus the current date.

slide-20
SLIDE 20

20

15-214

Dis9nguishing domain vs. implementa9on concepts

slide-21
SLIDE 21

21

15-214

Dis9nguishing domain vs. implementa9on concepts

  • Domain-level concepts:

– Almost anything with a real-world analogue

  • Implementa9on-level concepts:

– Implementa9on-like method names – Programming types – Visibility modifiers – Helper methods or classes – Ar9facts of design paderns

slide-22
SLIDE 22

22

15-214

Summary: Understanding the problem domain

  • Know your tools to build domain-level representa9ons

– Domain models – System sequence diagrams – System behavioral contracts

  • Be fast and (some9mes) loose

– Elide obvious(?) details – Iterate, iterate, iterate, …

  • Get feedback from domain experts

– Use only domain-level concepts

slide-23
SLIDE 23

23

15-214

Draw a domain model for cryptarithm solving

slide-24
SLIDE 24

24

15-214

Ar9facts of our design process

  • Model / diagram the problem, define objects

– Domain model (a.k.a. conceptual model)

  • Define system behaviors

– System sequence diagram – System behavioral contracts

  • Assign object responsibili9es, define interac9ons

– Object interac9on diagrams

  • Model / diagram a poten9al solu9on

– Object model

Understanding the problem Defining a solu9on

slide-25
SLIDE 25

25

15-214

Object-oriented programming

  • Programming based on structures

that contain both data and methods

public class Bicycle { private int speed; private final Wheel frontWheel, rearWheel; private final Seat seat; … public Bicycle(…) { … } public void accelerate() { speed++; } public int speed() { return speed; } }

slide-26
SLIDE 26

26

15-214

Responsibility in object-oriented programming

  • Data:

– Private or otherwise encapsulated data – Data in closely related objects

  • Methods:

– Private or otherwise encapsulated opera9ons – Object crea9on, of itself or other objects – Ini9a9ng ac9ons in other objects – Coordina9ng ac9vi9es among objects

slide-27
SLIDE 27

27

15-214

Using interac9on diagrams to assign object responsibility

  • For a given system-level opera9on, create an object interac9on

diagram at the implementa6on-level of abstrac9on

– Implementa9on-level concepts:

  • Implementa9on-like method names
  • Programming types
  • Helper methods or classes
  • Ar9facts of design paderns
slide-28
SLIDE 28

28

15-214

Example interac9on diagram #1

Use case scenario: A library member should be able to use her library card to log in at a library system kiosk and …

slide-29
SLIDE 29

29

15-214

Example interac9on diagram #2

Use case scenario: …and borrow a book. A3er confirming that the member has no unpaid late fees, the library system should determine the book’s due date by adding its loan period to the current day, and record the book and its due date as a borrowed item in the member’s library account.

slide-30
SLIDE 30

30

15-214

Interac9on diagrams help evaluate design alterna9ves

Create two possible interac9on diagrams:

1. Solving a cryptarithm, assuming that the cryptarithm class has responsibility for solving itself 2. Solving a cryptarithm, assuming that the main method (or a delegated method or class) has responsibility for solving the cryptarithm

slide-31
SLIDE 31

31

15-214

Heuris9cs for responsibility assignment

  • Controller heuris9c
  • Informa9on expert heuris9c
  • Creator heuris9c

Goals Heuristics Patterns Principles

slide-32
SLIDE 32

32

15-214

The controller heuris9c

  • Assign responsibility for all system-level behaviors to a single

system-level object that coordinates and delegates work to other

  • bjects

– Also consider specific sub-controllers for complex use-case scenarios

  • Design process: Extract interface from system sequence diagrams

– Key principles: Low representa9onal gap and high cohesion

slide-33
SLIDE 33

33

15-214

Informa9on expert heuris9c

  • Assign responsibility to the class that has the informa9on

needed to fulfill the responsibility

– Ini9aliza9on, transforma9on, and views of private data – Crea9on of closely related or derived objects

slide-34
SLIDE 34

34

15-214

Responsibility in object-oriented programming

  • Data:

– Private or otherwise encapsulated data – Data in closely related objects

  • Methods:

– Private or otherwise encapsulated opera9ons – Object crea9on, of itself or other objects – Ini9a9ng ac9ons in other objects – Coordina9ng ac9vi9es among objects

slide-35
SLIDE 35

35

15-214

Informa9on expert heuris9c

  • Assign responsibility to the class that has the informa9on

needed to fulfill the responsibility

– Ini9aliza9on, transforma9on, and views of private data – Crea9on of closely related or derived objects

  • Design process: Assignment from domain model

– Key principles: Low representa9onal gap and low coupling

slide-36
SLIDE 36

36

15-214

Use the informa9on expert heuris9c

  • In Homework 3, what object should have the responsibility to

solve a cryptarithm?

  • What is the relevant informa9on?
slide-37
SLIDE 37

37

15-214

Use the informa9on expert heuris9c

  • In Homework 3, what object should have the responsibility to

solve a cryptarithm?

  • What is the relevant informa9on?

– Who knows the # of digits (e.g. base 10) in the cryptarithm? – Who knows the leders of the cryptarithm? – Who can evaluate the cryptarithm expressions to check for equality?

slide-38
SLIDE 38

38

15-214

Another design principle: Minimize conceptual weight

  • Label the concepts for a proposed object

– Related to representa9onal gap and cohesion

slide-39
SLIDE 39

39

15-214

Creator heuris9c: Who creates an object Foo?

  • Assign responsibility of crea9ng an object Foo to a class that:

– Has the data necessary for ini9alizing instances of Foo – Contains, aggregates, or records instances of Foo – Closely uses or manipulates instances of Foo

  • Design process: Extract from domain model, interac9on diagrams

– Key principles: Low coupling and low representa9onal gap

slide-40
SLIDE 40

40

15-214

Use the creator heuris9c

  • In Homework 3, what object should have the responsibility for

crea9ng the permuta9on generator?

slide-41
SLIDE 41

41

15-214

Object-level ar9facts of this design process

  • Object interac9on diagrams add methods to objects

– Can infer addi9onal data responsibili9es – Can infer addi9onal data types and architectural paderns

  • Object model aggregates important design decisions

– Is an implementa9on guide

slide-42
SLIDE 42

42

15-214

Crea9ng an object model

  • Extract data, method names, and types from interac9on diagrams

– Include implementa9on details such as visibili9es

slide-43
SLIDE 43

43

15-214

slide-44
SLIDE 44

44

15-214

Create an object model for your cryptarithm solver

slide-45
SLIDE 45

45

15-214

Summary:

  • Object-level interac9on diagrams and object model

systema9cally guide the design process

– Convert domain model, system sequence diagram, and contracts to

  • bject-level responsibili9es
  • Use heuris9cs to guide, but not define, design decisions
  • Iterate, iterate, iterate…