Lösungsvorschlag Software Architecture Endterm 2007

Aus VISki
Wechseln zu: Navigation, Suche

1 Abstract Data Types (10 Points)

1.

(a)

new_cd

(b)

title, artist, price, quantity

(c)

set_price, sell, order_new

2.

new_cd, set_price, sell, order_new

3.

P1 new_cd(t, a, p, q) require p>0 and q>0 ["...a new CD can only be created if there is at least one copy on stock..."]
P2 set_price(c, x) require x > 0
P3 sell(c, x) require quantity(c) >= x [It seems reasonable to add "and x > 0" because of "“sell” reduces the quantity" even though it's not specifically stated elsewhere]
P4 order_new(c,x) require x > 0 and quantity(c)=0 ["..order new copies of a certain CD when there are none left on stock"]

4.

t: STRING (Title), a: STRING (Artist), p: INTEGER (Price), q: INTEGER (Quantity)

title(new_cd(t, a, p, q)) = t
artist(new_cd(t, a, p, q)) = a
price(new_cd(t, a, p, q)) = p
quantity(new_cd(t, a, p, q)) = q


title(set_price(c, p)) = title(c)
artist(set_price(c, p)) = artist(c)
price(set_price(c, p)) = p
quantity(set_price(c, p)) = quantity(c)


title(sell(c, q)) = title(c)
artist(sell(c, q)) = artist(c)
price(sell(c, q)) = price(c)
quantity(sell(c, q)) = quantity(c) - q


title(order_new(c, q)) = title(c)
artist(order_new(c, q)) = artist(c)
price(order_new(c, q)) = price(c)
quantity(order_new(c, q)) = q

2 Design by Contract (10 Points)

<eiffel> class

 COUNTER

feature increment

 ensure
   item = old item + 1

decrement

 require
   item > 0
 ensure
   item = old item -1

invariant

 item >= 0


class

 STUDENT

feature make

 require
   n /= void and then not n.is_empty
 ensure
   name = n

set_name

 require
   n /= void and then not n.is_empty
 ensure
   name = n

invariant

 n /= void and then not name.is_empty


class COURSE

feature make

 require
   n /= void and then not n.is_empty
 ensure
   name = n and then not n.is_empty and then count_students /= void and then students /= void

register

 require
   s /= void
   not students.has(s)
 ensure
   students.has(s) and count_students.item = old count_students.item + 1

delete

 require
   s /= void
   students.has(s)
 ensure
   not students.has(s) and count_students.item = old count_students.item - 1

invariant

 name /= void and then not name.is_empty
 students /= void
 count_students /= void
 count_students.item = students.count

</eiffel>

3 Design Pattern Categories (11 Points)

1. Creational design patterns

Singleton, Abstract Factory, Builder

2. Behavioral design patterns

Chain of Responsibility, Strategy, State

3. Structural design patterns

Composite, Flyweight, Decorator, Bridge

4 Observer (18 Points)

5 Concurrent Programming (17 Points)

5.1 True or false

T, T, F (??)

5.2 Busy waiting

http://en.wikipedia.org/wiki/Busy-wait

5.3 Semaphores

http://en.wikipedia.org/wiki/Semaphore_(programming) / http://en.wikipedia.org/wiki/Mutex

5.4 Programming

<eiffel> m: MUTEX -- access to queue s: SEMAPHORE -- number of jobs waiting create m.make(1) create s.make(0) </eiffel>

Printing server program:

<eiffel> new_task := next_print_task m.P store_task(new_task, buffer) m.V s.V(1) </eiffel>

Printer program

<eiffel> s.P(1) m.P print_task := task_from_buffer(buffer) remove_task(print_task, buffer) m.V print(print_task) </eiffel>