37
7/28/2019 MIT6_005f08_lec18 http://slidepdf.com/reader/full/mit6005f08lec18 1/37 MIT OpenCourseWare http://ocw.mit.edu 6.005 Elements of Software Construction Fall 2008 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

MIT6_005f08_lec18

Embed Size (px)

Citation preview

Page 1: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 1/37

MIT OpenCourseWarehttp://ocw.mit.edu

6.005 Elements of Software Construction

Fall 2008

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

Page 2: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 2/37

6.005elements ofsoftwareconstructionhow to design a photo catalog Daniel Jackson

Page 3: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 3/37

topics or today a problem

�  conceptual design of a photo organizer 

a new paradigm

�  computation over relational structures

� today, the abstract design level: object modelling

�  determines, in particular, model part of MVC (see last lecture)

object modelling

�  snapshot semantics

�  basic notation: domain/range, multiplicity, classification

�  some classic patterns

© Daniel Jackson 2008 2

Page 4: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 4/37

the problem

Page 5: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 5/37

problem

Screenshot of Adobe Photoshop Lightroom removed due to copyright restrictions.

In the Library view, you can select images to add or remove. The left-hand sidebar includes Collections that you can define.

design a photo cataloguing application

�  Lightroom, iView MediaPro, iPhoto, Aperture, Picasa, etc© Daniel Jackson 2008 4

Page 6: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 6/37

what kind o problem is this?

mostly about conceptual design�  what are the key concepts?

�  how are they related to one another?

�  what kinds of structures?

good conceptual design leads to

�  straightforward path to implementation

�  simplicity and flexibility in final product

© Daniel Jackson 2008 5

Page 7: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 7/37

why a new model?

why not use datatype productions?�  tree-like structures only: no sharing

�  immutable types only

why not state machines?

�  our catalog is a state machine

�  but the problem lies in the structure of the state

�  our state machine notation assumed simple states

a new approach: object models�  structure is a labelled graph

�  put another way: sets of objects + relations

© Daniel Jackson 2008 6

Page 8: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 8/37

the relational paradigm

computation is about �  actions, states, transitions

�  functions, expressions, values

�  and now: updates and queries on relations

why is this useful?

�  conceptual modeling

�  relational databases

�  object-oriented programming*

�  semantic web, document object models, etc

*for proposals to make relations explicit in object-oriented programming, see this survey: James Noble, Roles and Relationships, ECOOP 2007 Workshop on Roles and Relationships in Object-Oriented Programming, Multiagent Systems, and Ontologies; http://iv.tu-berlin.de/TechnBerichte/2007/2007-09.pdf

Page 9: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 9/37

basic OM notation

Page 10: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 10/37

snapshots

a snapshot or object diagram�  shows a single instance of a structure

example for photo organizer 

�  in this case, two sets

Photo (shown in beige)Collection (in grey)

�  and two relations

photos: Collection -> Photo

subs: Collection -> Collection

9

a collection

a relationship:

C0 is subcollection of C1

a photo

a relationship:

P0 in collection C2

© Daniel Jackson 2008

Page 11: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 11/37

more snapshots

how can we summarize this infinite set?

© Daniel Jackson 2008 10

Page 12: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 12/37

an object model

each box�  denotes a (maybe empty) set of objects

each arc 

�  denotes a relation, ie. set of links between objects

note

�  objects have no internal structure!

�  all structure is in the relations

exercise

�  draw a snapshot that the OM rules out

© Daniel Jackson 2008 11

Collection

Photo

subs

photos

Page 13: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 13/37

enriching the notation

what’s wrong with these snapshots?�  how would we rule them out?

key idea: multiplicity

�  measure the in-degree and out-degree of each relation

© Daniel Jackson 2008 12

Page 14: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 14/37

multiplicity

A BRm n

multiplicity markings�  on ends of relation arc

�  show relative counts

interpretation

�  R maps m A’s to each B

�  R maps each A to n B’s

marking/meaning

+ one or more* zero or more ! exactly one ? at most one omitted marking equivalent to *

© Daniel Jackson 2008 13

Page 15: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 15/37

R

kinds o unction

standard kinds of function�  easily expressed with multiplicities

?A B

A

R

BR

R is a function

!R is a total function

? !A B

A

R

BR

R is an injection

+ !R is a surjection

! !A B R is a bijection

© Daniel Jackson 2008 14

Page 16: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 16/37

multiplicity example

we’ve added naming�  always an important and subtle issue

�  is the multiplicity constraint desirable? necessary?

Collection

Photo

subs

photos

CollectionName

name

?!

*

*

?

© Daniel Jackson 2008 15

Page 17: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 17/37

classiying objects

suppose we to classify photos�  by file location: online, offline, missing

�  by selection: selected, focusPhoto

Photo

Offline MissingOnline

Selected

Focus

oval means

singleton set

© Daniel Jackson 2008 16

Page 18: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 18/37

classifcation syntax

can build a taxonomy of objects�  introduce subsets

�  indicate which are disjoint

�  and which exhaust the superset

A

B

A

B C

abstract   A 

B C

B 㱪 B 㱯 C = 㱵 B 㱮 C = A

© Daniel Jackson 2008 17

Page 19: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 19/37

relations on subsets

when placing a relation�  can place on subset

�  loose multiplicity is a hint

Photo Filepathpath!

Offline MissingOnline

Volume Date

vol since

! !

© Daniel Jackson 2008 18

Page 20: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 20/37

composite

a classic pattern�  hierarchical containment

�  file systems, org charts, network domains, etc

you’ve seen this with datatypes

�  technical differences though

�  OM allows cycles (but often rule out)

�  OM can say just one root

Collection

FolderFolio

subs

Photo

photos

© Daniel Jackson 2008 19

Page 21: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 21/37

hotel locking

Page 22: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 22/37

example: hotel locking

modelling physical, distributed state

state in OM need not represent 

�  a centralized store

�  data stored in a computer 

© Daniel Jackson 2008 21

Page 23: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 23/37

hotel locking

recodable locks (since 1980)�  new guest gets a different key

�  lock is ‘recoded’ to new key

�  last guest can no longer enter 

how does it work?

�  locks are standalone, not wired

Page 24: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 24/37

a recodable locking scheme

card has two keysif first matches lock,recode with second

if second matches,

 just open

Page 25: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 25/37

exercise

draw an object model�  showing the essential state of hotel locking

�  state includes front desk, locks, keys held by guests

review

�  did you exploit multiplicities? keys are all about uniqueness

�  did you include only the sets and relations that are needed?

�  are your sets really sets, or are some of them ‘singleton placeholders’?

�  do all your sets and relations have a clear interpretation?

�  where are the various parts of the state stored physically?

�  which relations are modifiable?

Page 26: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 26/37

a solution

Guest

Card

holds

Room

Key

key

occupies

fst, snd

?

!

!

Issued

g->r in occupies: guest g has checked in

for room r but has not yet checked out

k in Issued: key k has already beenissued by front desk on some card: usedto ensure that locks are always recodedwith fresh keys

some subtleties

�  guest may occupy more than one room

�  family members may have identical cards

Page 27: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 27/37

common errors

Desk

Key

issues

Card

Key

has

Hotel

Guest

guests

Key

SndFst

be wary of top-level singleton�  Desk and Hotel not needed

relations represent state, not actions� 

so issues is suspectneed enough information in state to support application�  has is not enough: need to know which key is first, second

scope of classification�  classification of keys into first and second, is by card, not global�  so need relation, not subsets to indicate the distinction

Page 28: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 28/37

colour palettes

Page 29: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 29/37

example: colour palettes

modelling the state of an application� how colours are organized

Screenshots of color schemes in the Keynote and PowerPoint

presentation programs removed due to copyright restrictions. 

essential idea

�  elements are coloured

�  can assign colour from palette

�  gives consistent appearance

© Daniel Jackson 2008 28

l b d l

Page 30: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 30/37

colour

colour

colour

palette object modelsthree subtly different approaches

�  think what happens when palette is modified�  hard vs. soft links: as in Unix

palette

Doc

Element

elements

Palette

Swatch

swatches

palette

Doc

Element

elements

Palette

Swatch

swatches

valuekey

co our

palette

Colour

value

Name

key

Colour

value

coco our

Doc

elements

Palette

Swatch

swatches

Element

Name

keyco our

Name Colour

“Every problem in computer science can be solved by introducing another level of indirection”

-- David Wheeler

© Daniel Jackson 2008 29

Page 31: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 31/37

completing the organizer

i l

Page 32: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 32/37

issues to resolvecan collections hold photos and subcollections?

� decision: yes, so not Composite pattern

how are “all photos” in catalog represented?

�  decision: introduce non-visible root collection

unique collection names?�  decision: file system style, so siblings have distinct names

do parents hold children’s photos?

�  in logic: all c: Collection | c.subs.photos in c.photos ?

� decision: use two relations instead

c.inserted: the photos explicitly inserted into collection c

c.photos: the photos in collection c implicitly and explicitly

invariant relates these: c.photos = c.inserted + c.subs.photos© Daniel Jackson 2008 31

f l bj d l

Page 33: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 33/37

fnal object model

Collection

Visible

subs

Photo

inserted,

photos

Root Selected

Imageimage

!

CollectionName

name!

!

additional constraints

�  all collections reachable from root (implies acyclic)

Collection in Root.*subs

�  implicit photos are inserted photos plus photos in subcollections

all c: Collection | c.photos = c.inserted + c.subs.photos

�  names unique within parent

all c: Collection | no c1, c2: c.subs | c1 != c2 and c1.name = c2.name© Daniel Jackson 2008 32

Page 34: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 34/37

modeling hints

hi

Page 35: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 35/37

hintshow to pick sets

� be as abstract as possible (thus Name, not String; SSN, not Number)

�  but values to be compared must have same type (so Date, not Birthday)

�  beware of singletons -- often a sign of code thinking

how to pick relations

�  represent state, not actions (so atFloor: Elevator->Floor, not arrives)

�  direction is semantic; doesn’t constrain ‘navigation’

choosing names

�  choose names that make interpretation clear �  include a glossary explaining what relations and sets mean

© Daniel Jackson 2008 34

Page 36: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 36/37

summary

i i l

Page 37: MIT6_005f08_lec18

7/28/2019 MIT6_005f08_lec18

http://slidepdf.com/reader/full/mit6005f08lec18 37/37

principles

data before function

�  before thinking about system function, think about data

an object model is an invariant 

�  meaning is set of structured states

�  declared sets + subset relationships + relations between sets + multiplicities

�  augment diagram with textual constraints (in Alloy, as above, or just English)

model objects are immutable

� all state kept in subsets and relations

�  model objects have no ‘contents’

�  important to keep coding options open